教程:用 qmake 使用内部库
你可以在子目录项目中添加一个库。使用向导创建项目和库,并将库链接到项目中。
注意: 本教程仅适用于选择 qmake 作为 subdirs 项目构建系统的情况。
创建共享库
创建共享库
- 转到File >New Project ,然后选择Library >C++ Library 。如果顶层项目是子目录项目或包含子目录项目,则可以将库添加到项目中。不过,这并不能将项目中的其他库与之链接。
- 选择Choose ,打开Project Location 对话框。
- 在Name 中,为库命名。例如,MyLibrary。
- 选择Next (Windows 和 Linux)或Continue (macOS),打开Define Build System 对话框。
- 在Build system 中,选择qmake 。
- 选择Next 或Continue ,打开Define Project Details 对话框。
- 选择库类型,并输入要生成源代码文件的类的相关信息:类名、Qt 模块以及源文件和头文件名称。
- 选择Next 或Continue ,直至进入Project Management 对话框。在Add as a subproject to project 列表中,选择一个子目录项目。例如,MyApplication。
将应用程序链接到库
将项目链接到共享库:
- 在项目视图中,右键单击项目名称打开上下文菜单,然后选择Add Library >Internal Library >Next 。
向导会指示构建系统将现有应用程序项目或库项目与所选库进行链接。在此,您将添加上文创建的库。
- 在Library 中,选择mylibrary,然后选择Next 。
- 选择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.