There are a few questions which seem to be similar, but nothing really helps me out. I want to create a static library inside a project and use it in the same project, but linking error occurs.
A good example, which meets my conditions very well is attached to the Qt Ticket QTBUG-45706 https://bugreports.qt.io/browse/QTBUG-45706. In a simple explanation, we have an app which should use some self-made libraries. Just modifiy a few things to see my problem.
app -> main.cpp
#include <QCoreApplication>
#include <lib.h>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
Lib l1;
return a.exec();
}
lib.pro
CONFIG += staticlib
If you now compile the project, you will see the following error
main.obj:-1: error: LNK2019: unresolved external symbol "__declspec(dllimport) public: __cdecl Lib2::Lib2(void)" (__imp_??0Lib2@@QEAA@XZ) referenced in function main
Use Qt Creator 4.0.3 based on Qt 5.6.1, qmake with mscv2013
What is needed to bring this to work?
CLARIFY:
The project structure is as follow:
subdirs_test.pro (subdir project)
\- app (app project, includes lib and lib2)
\-- app.pro
\-- main.cpp
\- lib (static library)
\-- lib.pro
\-- lib.h
\-- lib_global.h
\-- lib.cpp
\- lib2 (static library)
\-- lib2.pro
\-- lib2.h
\-- lib2_global.h
\-- lib2.cpp
The 'app' project should use the classes from lib and lib2, which are static libraries.
As suggested, use the "Add Library..." doesn't change a thing. In my case, this code will be generated.
win32:CONFIG(release, debug|release): LIBS += -L$$OUT_PWD/../lib/release/ -llib
else:win32:CONFIG(debug, debug|release): LIBS += -L$$OUT_PWD/../lib/debug/ -llib
else:unix: LIBS += -L$$OUT_PWD/../lib/ -llib
INCLUDEPATH += $$PWD/../lib
DEPENDPATH += $$PWD/../lib
win32-g++:CONFIG(release, debug|release): PRE_TARGETDEPS += $$OUT_PWD/../lib/release/liblib.a
else:win32-g++:CONFIG(debug, debug|release): PRE_TARGETDEPS += $$OUT_PWD/../lib/debug/liblib.a
else:win32:!win32-g++:CONFIG(release, debug|release): PRE_TARGETDEPS += $$OUT_PWD/../lib/release/lib.lib
else:win32:!win32-g++:CONFIG(debug, debug|release): PRE_TARGETDEPS += $$OUT_PWD/../lib/debug/lib.lib
else:unix: PRE_TARGETDEPS += $$OUT_PWD/../lib/liblib.a