如何在CMake中正确创建目标之间的依赖关系? [英] How do I correctly create dependencies between targets in 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.到目前为止,我可以将其获取到工作,但我必须在
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
链接,并具有其包含目录。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项目中添加此功能,然后填写要在主项目中看到的包含目录。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.PROJECT,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)
DEPENDENCY,CMakeLists.txt
DEPENDENCY, CMakeLists.txt
project (Dependency) add_library (Dependency SomethingToCompile.cpp) target_include_directories (Dependency PUBLIC include)
这篇关于如何在CMake中正确创建目标之间的依赖关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!