4

I am developing an application in QT Creator in c++ on Linux

I have created my own library so that I can use some common classes throughout a set of applications.

In the library I have created I have used another external static library (libSDL.a). I have configured my library to a static library (*.a) and it compiles with no problems.

I then added my library to another application and used some of the classes. When trying to compile my application I am getting undefined references from within my library to function calls to the other library.

From my understanding, static libraries are suppose to be copied in during compilation. Why am I getting the undefined references to a library that should be copied into my library?

Here is how the library project is configured in the *.pro file:

QT -= gui
TARGET = FoobarTools
TEMPLATE = lib
CONFIG += staticlib
CONFIG -= shared
DEFINES += FOOBARTOOLS_LIBRARY
INCLUDEPATH += ./include/SDL_Headers/
LIBS += -L./bin/ -lSDL
SOURCES += ...
HEADERS += ...

Here is how my application *.pro file is using my library:

QT -= gui
TARGET = FoobarApp
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
INCLUDEPATH += ./include/
LIBS += -L./bin/ -lFoobarTools
SOURCES += ...
HEADERS += ...
user1433456
  • 41
  • 1
  • 3

3 Answers3

1

In the application's .pro you need:

INCLUDEPATH += LibraryPath (This points to the header-file's directory.)

DEPENDPATH += LibraryPath (This also points to the header-file's directory.)

LIBS += -LDebugOrReleasePath -lLibraryName (This is the lib-filename minus 'lib' at the beginning and '.a' at the end.)

Once that's done check if the #includes to your custom library are still working.

In the static libary's .pro file you dont need to touch anything, maybe add 'CONFIG += release'.

A4-
  • 11
  • 2
1

Both your library and the library it's using should be linked in the application.

INCLUDEPATH += ./include/SDL_Headers/
INCLUDEPATH += ./include/
LIBS += -L./bin/ -lFoobarTools
LIBS += -L./bin/ -lSDL
//And dont forget the Target dependencies.
PRE_TARGETDEPS += ./libFoobarTools.a
PRE_TARGETDEPS ./libSDL.a

If you want to find out more about the reason the library compiles but not the application check out this question.

Community
  • 1
  • 1
Eaton Emmerich
  • 390
  • 3
  • 12
-1

I think you can find the answer here:

http://gcc.gnu.org/ml/gcc-help/2004-04/msg00104.html

and, more precisely in this follow-up:

http://gcc.gnu.org/ml/gcc-help/2004-04/msg00106.html

I cannot test it right now, but I think the probable cause might be resumed by this:

"linker will "throw away" a library if it comes across it but none of the symbols it defines are needed"

As a first-aid help, add the -lSDL to your second .pro file.

Edit: are you sure that your static library (the first .pro file) really uses some symbols from libSDL? If not, then the compiler will simply ignore the libSDL.a file and will not include it in your static library. Even if you use some symbols from libSDL.a, only those functions will be copied into the executable, while the other symbols would be not (at least, this is what I think). "Static libraries have special rules when it comes to linking. An object from the static library will only added to the binary if the object provides an unresolved symbol." (see: https://stackoverflow.com/a/2649792/1284631). Then, if your executable (the second .pro file) makes use of some not-copied symbols from libSDL, you will have the errors. Quoting the same source: "On Linux, you can change that behavior with the --whole-archive linker option: g++ -Wl,--whole-archive some_static_lib.a -Wl,--no-whole-archive". This way you make sure that you carry the whole static libSDL.a archive within yours.

Community
  • 1
  • 1
user1284631
  • 4,038
  • 30
  • 58