C++ 如何在 CMake 中正确创建目标之间的依赖关系?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4905089/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I correctly create dependencies between targets in CMake?
提问by radman
I am trying to use CMake to set up some simple dependencies between a C++ project and the libraries that it uses.
我正在尝试使用 CMake 在 C++ 项目和它使用的库之间设置一些简单的依赖关系。
The set up is as follows
设置如下
- Project
- Dependency
- 项目
- 依赖
Project itself contains source files that include headers from Dependency
and when the executable is built it needs to be linked against Dependency
's static library.
项目本身包含包含头文件的源文件,Dependency
当可执行文件被构建时,它需要链接到Dependency
的静态库。
So far I can get this to work, but I have to specify the include directories of Dependency
in the CMakeLists.txt
file for Project
manually. I want this to be pulled out automatically, and I have explored the option of using the find_package()
command to do so with limited success and making things much more complicated.
到目前为止,我能得到这个工作,但我必须指定包括目录Dependency
中CMakeLists.txt
的文件Project
手动。我希望它被自动拉出,并且我已经探索了使用find_package()
命令来这样做的选项,但成功率有限,并使事情变得更加复杂。
All I want to do is have Dependency
built before Project
and have Project
link against the library and have its include directories. Is there a simple concise way of achieving this?
我想要做的就是之前Dependency
建立Project
并Project
链接到库并拥有它的包含目录。有没有简单简洁的方法来实现这一目标?
My current CMake files:
我当前的 CMake 文件:
Project
, file CMakeLists.txt
:
Project
,文件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)
Dependency
, file CMakeLists.txt
:
Dependency
,文件CMakeLists.txt
:
project(Dependency)
add_library(Dependency SomethingToCompile.cpp)
target_link_libraries(Dependency)
回答by dipp
Since CMake 2.8.11
you can use target_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.
由于CMake 2.8.11
您可以使用target_include_directories
. 只需在您的 DEPENDENCY 项目中添加此功能并填写您希望在主项目中看到的包含目录即可。CMake 会关心其余的。
PROJECT, CMakeLists.txt:
项目,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)
DEPENDENCY, CMakeLists.txt
依赖,CMakeLists.txt
project (Dependency)
add_library (Dependency SomethingToCompile.cpp)
target_include_directories (Dependency PUBLIC include)
回答by Vladislav Vaintroub
It is not exactly clear what you want to do, and why Project
and Dependency
have to be build separately.
目前尚不清楚您想要做什么,以及为什么Project
和Dependency
必须单独构建。
My first though on your example would be
我的第一个例子是
In
PROJECT
'sCMakeLists.txt
:- Remove
add_dependencies(Project Dependency)
There is no need to specify dependency,target_link_libraries()
already does that.
- Remove
In
DEPENDENCY
'sCMakeLists.txt
:- Remove
project(Dependency)
It builds a library, so why to have own project? - Remove
target_link_libraries(Dependency)
Because it does nothing
- Remove
在
PROJECT
的CMakeLists.txt
:- 删除
add_dependencies(Project Dependency)
不需要指定依赖项,target_link_libraries()
已经这样做了。
- 删除
在
DEPENDENCY
的CMakeLists.txt
:- Remove
project(Dependency)
它建了一个库,那为什么要有自己的项目呢? - 删除
target_link_libraries(Dependency)
因为它什么都不做
- Remove