本页内容

教程:在 qmake 中使用内部库

您可以将库添加到subdirs项目中。使用向导创建项目和库,并将库链接到项目。

注意:本教程 仅适用于将 qmake 选为 subdirs 项目的构建系统的情况。

创建共享库

要创建共享库:

  1. 转到“File ” > “New Project ”,然后选择“Library ” > “C++ Library ”。如果您的顶级项目是子目录项目或包含子目录项目,您可以将库添加到该项目中。但是,这不会将项目中的其他库与该库进行链接。
  2. 选择“Choose ”以打开“Project Location ”对话框。

    “项目位置”对话框

  3. 在“Name ”中,为库命名。例如,MyLibrary
  4. 选择“Next ”(Windows 和 Linux 系统)或“Continue ”(macOS 系统)以打开“Define Build System ”对话框。
  5. Build system 中,选择qmake
  6. 选择“Next ”或“Continue ”以打开“Define Project Details ”对话框。

    “定义项目详细信息”对话框

  7. 选择库类型,并输入要为其生成源代码文件的类的相关信息:类名、Qt 模块以及源文件和头文件名。
  8. 反复点击“Next ”或“Continue ”,直到进入“Project Management ”对话框。在“Add as a subproject to project ”列表中,选择一个subdirs项目。例如,MyApplication

要将项目链接到共享库:

  1. “Projects”视图中,右键单击项目名称以打开上下文菜单,然后选择“Add Library ” > “Internal Library ” > “Next ”。

    向导会指示构建系统将现有应用程序项目或库项目与所选库进行链接。在此,您需要添加上面创建的库。

    添加内部库

  2. 在“Library ”中,选择“mylibrary”,然后选择“Next ”。
  3. 选择“Finish ”以将库声明添加到项目文件中。

向导会将以下库声明添加到.pro 文件中:

win32:CONFIG(release, debug|release): LIBS += -L$$OUT_PWD/../../../projects/mylib/release/ -lmylib
else:win32:CONFIG(debug, debug|release): LIBS += -L$$OUT_PWD/../../../projects/mylib/debug/ -lmylib
else:unix: LIBS += -L$$OUT_PWD/../../../projects/mylib/ -lmylib
INCLUDEPATH += $$PWD/../../../projects/mylib
DEPENDPATH += $$PWD/../../../projects/mylib
win32:CONFIG(release, debug|release): PRE_TARGETDEPS += $$OUT_PWD/../../../projects/mylib/release/mylib.lib
else:win32:CONFIG(debug, debug|release): PRE_TARGETDEPS += $$OUT_PWD/../../../projects/mylib/debug/mylib.lib
else:unix: PRE_TARGETDEPS += $$OUT_PWD/../../../projects/mylib/libmylib.a

另请参阅 “向 qmake 项目添加库”“向 CMake 项目添加库”“向项目添加子项目”、“选择构建系统”、“使用项目向导”以及“创建项目”

Copyright © The Qt Company Ltd. and other contributors. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.