将库添加到 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")另请参阅 《如何:使用 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.