1

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
sandkasten
  • 603
  • 1
  • 8
  • 21
  • 1
    It's not clear what you are trying to do. you have to set up at least 2 .pro files. One for the lib and one for the app. in the app one add the library output path to the `INCLUDES` and `LIBS` variables. Normally you also have a 3rd subdir project to tie up all together – IlBeldus Jul 14 '17 at 10:16
  • 1
    [DONT use the Q_DECL_IMPORT macro with static libraries](https://forum.qt.io/topic/25328/static-library/8), the macros are only used in shared windows libraries. This in an important note from one of my co-worker. Often the macro is hidden in some other global header file. – sandkasten Jul 18 '17 at 10:08

2 Answers2

1

Can you try doing next steps:

  1. Right button on project
  2. Add library
  3. Choose type (external or other)
  4. Set flag on static, like this picture
needo
  • 98
  • 9
  • Already tried this, doesn't change a thing. An extra bunch of code is generated but linker error is still there. I can add the generated code if this hepls. – sandkasten Jul 14 '17 at 10:35
  • update my question with result, because its too long for comment – sandkasten Jul 14 '17 at 10:46
  • @sandkasten can you try make one .pro and any .pri for you libs, and in .pro write something like `include($$PWD/lib1.pri)` and write `LIBS +=` in this .pri files – needo Jul 14 '17 at 10:57
  • Sorry, I'm quite new to Qt and doesn't know what you exactly mean.. can you explane me what to do? – sandkasten Jul 14 '17 at 11:03
  • @sandkasten check information [this](https://stackoverflow.com/questions/8358627/qt-pro-vs-pri), [this](https://stackoverflow.com/questions/718447/adding-external-library-into-qt-creator-project), [this](https://stackoverflow.com/questions/1361229/using-a-static-library-in-qt-creator) and [this](https://stackoverflow.com/questions/20588440/include-path-for-adding-an-external-library-in-qt-creator) :) – needo Jul 14 '17 at 11:12
1

You can use QtCreators Subdirs project. Here's a detailed step by step instructions how to achieve that with QtCreator.

  • Pick Subdirs Project from the New Project wizard menu.

Subdirs Project

  • Add Subrojects by clicking on created Subdirs project with right mouse button and selecting New Subproject....

New Subproject

  • By following wizards you should have a GUI or console Subproject and a library Subproject. Then click on subproject where you want to link your library subproject with right mouse button and select Add Library....

Add Library

  • Select Internal library in the dialog and you will be prompted to choose library you want to add.

Internal library

  • Make sure your library subproject is included before gui/console subproject as subdir project will fail to build.

    TEMPLATE = subdirs

    SUBDIRS += \ LibProject \ CoreProject

Eligijus Pupeikis
  • 1,115
  • 8
  • 19
  • Thanks for this detailed description, this is very helpfull! Unfortunately this doesn't work, can't find depend... But after adding `CoreProject.depends = LibProject` it works. The intresstig question is, why doesn't it work in my own project? I will double check everything and see if I can find any difference. – sandkasten Jul 14 '17 at 11:19
  • @sandkasten Have you done the last step changing `SUBDIRS` include order. Because QtCreator generates pre target dependancies `PRE_TARGETDEPS += $$OUT_PWD/../LibProject/debug/LibProject.lib` and not meeting them your build will fail. – Eligijus Pupeikis Jul 14 '17 at 11:28
  • Yep, I changed the `SUBDIRS` include order, but if I remove the depend, the build failed. But I think this is my main problem, for some reason the build order is not correct and the lib will not be build and can't be linked. But the `PRE_TARGETDEPS` is there (in the CoreProject) – sandkasten Jul 14 '17 at 12:17
  • Qt reported the error `:-1: error: dependent '../Lib/release/Lib.lib' does not exist.` So the project expects the Lib.lib file but it will not be build. Do know any way to force the build? – sandkasten Jul 14 '17 at 12:24
  • 1
    Right click on each of the subprojects and click build. Sometimes subdir project can be a little bit buggy. Have you followed my instructions and created minimal subdirs project with library and application subprojects? Did it give you the same error? [mcve] – Eligijus Pupeikis Jul 14 '17 at 12:36
  • 1
    Make also sure to rerun qmake every time you change something in a .pro file – IlBeldus Jul 14 '17 at 12:46
  • Wont work :/ Yep, I take the time to create the minmal project (this need the extra depends for me) and every comment from me here refers to the minimal project. – sandkasten Jul 14 '17 at 12:58
  • Good advice, but I always delete the hole build folder to not match any problems with build artefacts. – sandkasten Jul 14 '17 at 13:03
  • I will accept this as answer, because it brings me on the right track and helps me best to solve my existing problem. After changing the `SUBDIRS` order and include the `*.depends =` tag in the pro file, everything works as expected. – sandkasten Jul 18 '17 at 10:01