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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 16:53:34  来源:igfitidea点击:

How do I correctly create dependencies between targets in CMake?

c++buildcmake

提问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 Dependencyand 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 Dependencyin the CMakeLists.txtfile for Projectmanually. 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.

到目前为止,我能得到这个工作,但我必须指定包括目录DependencyCMakeLists.txt的文件Project手动。我希望它被自动拉出,并且我已经探索了使用find_package()命令来这样做的选项,但成功率有限,并使事情变得更加复杂。

All I want to do is have Dependencybuilt before Projectand have Projectlink against the library and have its include directories. Is there a simple concise way of achieving this?

我想要做的就是之前Dependency建立ProjectProject链接到库并拥有它的包含目录。有没有简单简洁的方法来实现这一目标?

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.11you 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 Projectand Dependencyhave to be build separately.

目前尚不清楚您想要做什么,以及为什么ProjectDependency必须单独构建。

My first though on your example would be

我的第一个例子是

  1. In PROJECT's CMakeLists.txt:

    • Remove add_dependencies(Project Dependency)There is no need to specify dependency, target_link_libraries()already does that.
  2. In DEPENDENCY's CMakeLists.txt:

    • Remove project(Dependency)It builds a library, so why to have own project?
    • Remove target_link_libraries(Dependency)Because it does nothing
  1. PROJECTCMakeLists.txt

    • 删除add_dependencies(Project Dependency)不需要指定依赖项,target_link_libraries()已经这样做了。
  2. DEPENDENCYCMakeLists.txt

    • Remove project(Dependency)它建了一个库,那为什么要有自己的项目呢?
    • 删除target_link_libraries(Dependency)因为它什么都不做