简体   繁体   中英

windows – Qt create and use self-made static library

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

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

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.

子目录项目

  • Add Subrojects by clicking on created Subdirs project with right mouse button and selecting 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... .

添加图书馆

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

内部图书馆

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

    TEMPLATE = subdirs

    SUBDIRS += \\ LibProject \\ CoreProject

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM