How do I correctly create dependencies between targets in CMake?(如何在 CMake 中正确创建目标之间的依赖关系?)
问题描述
我正在尝试使用 CMake 在 C++ 项目与其使用的库之间设置一些简单的依赖关系.
I am trying to use CMake to set up some simple dependencies between a C++ project and the libraries that it uses.
设置如下
- 项目
- 依赖
项目本身包含的源文件包含来自
Dependency
的头文件,并且在构建可执行文件时,它需要链接到Dependency
的静态库.Project itself contains source files that include headers from
Dependency
and when the executable is built it needs to be linked againstDependency
's static library.到目前为止我可以让它工作,但我必须在
Project
CMakeLists.txt文件中指定Dependency
的包含目录> 手动.我希望它被自动拉出,并且我已经探索了使用find_package()
命令这样做的选项,但成功率有限,并使事情变得更加复杂.So far I can get this to work, but I have to specify the include directories of
Dependency
in theCMakeLists.txt
file forProject
manually. I want this to be pulled out automatically, and I have explored the option of using thefind_package()
command to do so with limited success and making things much more complicated.我想要做的就是在
Project
之前构建Dependency
并具有针对库的Project
链接及其包含目录.有没有简单简洁的方法来实现这一点?All I want to do is have
Dependency
built beforeProject
and haveProject
link against the library and have its include directories. Is there a simple concise way of achieving this?我当前的 CMake 文件:
My current CMake files:
项目
,文件CMakeLists.txt
:cmake_minimum_required (VERSION 2.6) project (Project) include_directories ("${PROJECT_SOURCE_DIR}/Project") add_subdirectory (Dependency) add_executable (Project main.cpp) target_link_libraries (Project Dependency) add_dependencies(Project Dependency)
依赖
,文件CMakeLists.txt
:project(Dependency) add_library(Dependency SomethingToCompile.cpp) target_link_libraries(Dependency)
推荐答案
从
CMake 2.8.11
开始,您可以使用target_include_directories
.只需在您的 DEPENDENCY 项目中添加此功能并填写您希望在主项目中看到的包含目录即可.CMake 会关心其余的.Since
CMake 2.8.11
you can usetarget_include_directories
. Just simply add in your DEPENDENCY project this function and fill in include directories you want to see in the main project. CMake will care the rest.项目,CMakeLists.txt:
PROJECT, CMakeLists.txt:
cmake_minimum_required (VERSION 2.8.11) project (Project) include_directories (Project) add_subdirectory (Dependency) add_executable (Project main.cpp) target_link_libraries (Project Dependency)
依赖,CMakeLists.txt
DEPENDENCY, CMakeLists.txt
project (Dependency) add_library (Dependency SomethingToCompile.cpp) target_include_directories (Dependency PUBLIC include)
这篇关于如何在 CMake 中正确创建目标之间的依赖关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 CMake 中正确创建目标之间的依赖关系?
基础教程推荐
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- 从 std::cin 读取密码 2021-01-01
- 为什么语句不能出现在命名空间范围内? 2021-01-01
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- 使用从字符串中提取的参数调用函数 2022-01-01
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01
- Windows Media Foundation 录制音频 2021-01-01