I create in Qt Creator, but I think that the problem is not in it ...

I create static lib1 library in Qt without using QtCore. The project is obtained, by default, containing the files lib1.cpp and lib1.h, in which the empty class Lib1 is described (only the empty constructor).

I create a test application "Project without Qt", which is called test, connect this lib to it - everything works.

Now I create, like lib1, the static library lib2.

lib2.cpp:

#include "lib1.h" #include "lib2.h" Lib2::Lib2() { Lib1 *lib1 = new Lib1(); } 

I compile library Lib2 - while everything works as it should.

I insert a line into my application:

 Lib2 *lib2 = new Lib2(); 

And then a strange error appears:

Undefined reference to Lib1 :: Lib1 () in the file lib2.cpp

What a strange mistake? What am I doing wrong?


 TEMPLATE = app CONFIG += console c++11 CONFIG -= app_bundle CONFIG -= qt SOURCES += main.cpp unix:!macx: LIBS += -L$$PWD/../lib1/ -llib1 INCLUDEPATH += $$PWD/../lib1 DEPENDPATH += $$PWD/../lib1 unix:!macx: PRE_TARGETDEPS += $$PWD/../lib1/liblib1.a unix:!macx: LIBS += -L$$PWD/../lib2/ -llib2 INCLUDEPATH += $$PWD/../lib2 DEPENDPATH += $$PWD/../lib2 unix:!macx: PRE_TARGETDEPS += $$PWD/../lib2/liblib2.a 
  • Show .pro files. - maestro
  • TEMPLATE = app CONFIG + = console c ++ 11 CONFIG - = app_bundle CONFIG - = qt SOURCES + = main.cpp unix:! Macx: LIBS + = -L $$ PWD /../ lib1 / -llib1 INCLUDEPATH + = $ $ PWD /../lib1 DEPENDPATH + = $$ PWD /../lib1 unix:! Macx: PRE_TARGETDEPS + = $$ PWD /../ lib1 / liblib1.a unix:! Macx: LIBS + = -L $$ PWD /../ lib2 / -llib2 INCLUDEPATH + = $$ PWD /../ lib2 DEPENDPATH + = $$ PWD /../ lib2 unix:! Macx: PRE_TARGETDEPS + = $$ PWD /../ lib2 / liblib2. a - sitev_ru

1 answer 1

It turns out ... The order of linking libraries is important. If you link from lib1 first and then from lib2, and there are no references to lib1 in test, then what you see will be. It is necessary either to organize the libraries so that they first go to those that access external functions, and then those that define these functions.

  • 3
    The classic work of the ld linker algorithm, described along and across. - AnT