在 CMake 项目中添加库
除 Qt 库外,您还可以在项目中添加其他库。添加过程取决于库的类型和位置:
- 系统库
- 你自己的库
- 第三方库
Qt Creator 一旦您的项目成功构建并链接到所添加的库,系统将支持代码自动补全和语法高亮显示。
添加自己的库
使用qt_add_library命令在 CMakeLists.txt 文件中创建一个库并与之链接。
指定该库是静态链接还是动态链接。对于静态链接的内部库,在 CMakeLists.txt 项目文件中添加 CMake: target_link_libraries 命令来指定依赖关系。
添加外部库
通过外部库,Qt Creator 可以支持代码自动补全和语法高亮,就好像代码是当前项目或 Qt 库的一部分。
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")
另请参阅 "如何:使用 CMake 构建"、"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.