QT Creator - Using External Libraries in My Own Library

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

I created my own library so that I can use some common classes in all sets of applications.

In the library that I created, I used another external static library (libSDL.a). I configured my library in a static library (* .a) and it compiles without problems.

Then I added my library to another application and used some of the classes. When I try to compile my application, I get undefined links from my library to call functions in another library.

From my point of view, during compilation it is supposed to copy static libraries. Why do I get undefined links to a library that should be copied to my library?

This 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 += ... 

This is how my * .pro application file uses my library:

 QT -= gui TARGET = FoobarApp CONFIG += console CONFIG -= app_bundle TEMPLATE = app INCLUDEPATH += ./include/ LIBS += -L./bin/ -lFoobarTools SOURCES += ... HEADERS += ... 
+4
source share
3 answers

In the .pro application you need:

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

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

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

After that, check if #includes continues to your user library.

You do not need to touch anything in the static libary.pro file, perhaps add 'CONFIG + = release'.

+1
source

Both your library and the library it uses must 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 know more about the reason for compiling the library, but not about the application, this question is .

0
source

I think you can find the answer here:

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

and, more precisely, in this observation:

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

I cannot verify this right now, but I think the probable cause can be resumed as follows:

the linker will throw out the library if it encounters it, but none of the characters that it defines are required "

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

Edit: Are you sure your static library (the first .pro file) really uses some of the symbols from libSDL? If not, the compiler will simply ignore the libSDL.a file and not include it in your static library. Even if you use some of the symbols from libSDL.a, only these functions will be copied to the executable, while other symbols will not (at least, this is what I think). "Static libraries have special rules when it comes to references. An object from a static library will only be added to the binary if the object provides an unresolved character." (see fooobar.com/questions/139558 / ... ). Then, if your executable file (the second .pro file) uses some not copied symbols from libSDL, you will have errors. Quoting the same source: β€œOn Linux, you can change this behavior using the -whole-archive linker option: g ++ -Wl, - whole-archive some_static_lib.a -Wl, - no-whole-archive.” This way you will ensure that you carry the entire static libSDL.a archive in your own.

-1
source

Source: https://habr.com/ru/post/1415816/


All Articles