为 CMake 项目添加库
使用 CMake,您可以
- 从磁盘将现有的可执行文件或库文件导入 CMake 项目。
- 在构建中加入依赖项,如其他项目、资产和工件。
更多信息,请参阅《CMake:导入和导出指南》和《CMake:使用依赖关系指南》。
导入现有库
使用qt_add_library命令创建一个库,并在CMakeLists.txt 文件中与之链接,具体做法请参阅《构建项目》。
指定该库是静态链接还是动态链接。对于静态链接的内部库,在CMakeLists.txt 项目文件中添加CMake: target_link_libraries 命令以指定依赖关系。
使用依赖库
通过外部库,Qt Creator 可以支持代码自动补全和语法高亮,就好像代码是当前项目或 Qt XML 库的一部分。
Qt Creator 使用CMake: find_package 命令检测外部库。有些库是 CMake 安装时自带的。您可以在 CMake 安装的Modules 目录中找到这些库。更多信息,请参阅CMake: cmake-packages(7)。
查找 CMake 模块文件
对于有外部依赖关系的 CMake 项目,可使用Find<package>.cmake模块来暴露导入的目标。您可以使用预定义的sample_find_module 代码段将示例命令添加到.cmake 文件中。然后,您可以根据需要更改命令。
将Find 模块放到${CMAKE_CURRENT_SOURCE_DIR}/cmake 目录中,并将目录名追加到CMAKE_MODULE_PATH 列表变量中。例如
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/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.