简体   繁体   English

Windows – Qt创建并使用自制的静态库

[英]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 . Qt票证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 应用-> main.cpp

#include <QCoreApplication>
#include <lib.h>

int main(int argc, char *argv[])
{
  QCoreApplication a(argc, argv);

  Lib l1;

  return a.exec();
}

lib.pro 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 在mscv2013中使用基于Qt 5.6.1,qmake的Qt Creator 4.0.3

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. “ app”项目应使用lib和lib2中的类,它们是静态库。

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. 您可以使用QtCreators Subdirs项目。 Here's a detailed step by step instructions how to achieve that with QtCreator. 这是详细的逐步说明,说明如何使用QtCreator来实现。

  • Pick Subdirs Project from the New Project wizard menu. 从“ New Project向导菜单中选择“ Subdirs Project ”。

子目录项目

  • Add Subrojects by clicking on created Subdirs project with right mouse button and selecting New Subproject... . 通过使用鼠标右键单击已创建的Subdirs项目并选择New Subproject...来添加Subrojects。

新子项目

  • By following wizards you should have a GUI or console Subproject and a library Subproject. 通过遵循向导,您应该有一个GUI或控制台子项目和一个库子项目。 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. 确保您的库子项目包含在gui / console子项目之前,因为subdir项目将无法生成。

    TEMPLATE = subdirs 模板=子目录

    SUBDIRS += \\ LibProject \\ CoreProject SUBDIRS + = \\ LibProject \\ CoreProject

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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