English 中文(简体)
如何在CMake中正确创建目标之间的依赖关系?
原标题:How do I correctly create dependencies between targets in CMake?

我试图使用CMake在C++项目和它使用的库之间建立一些简单的依赖关系。

设置如下

  • Project
    • Dependency

项目本身包含源文件,这些源文件包含Dependency的头文件,并且在生成可执行文件时,需要将其链接到Dependency的静态库。

到目前为止,我可以做到这一点,但我必须手动在ProjectCMakeLists.txt文件中指定Dependency包含目录。我希望它能自动退出,并且我已经探索了使用find_package()命令这样做的选项,但成功率有限,而且会使事情变得更加复杂。

我所想做的就是在Project之前构建Dependency,并使Project链接到库及其包含目录。有没有一种简单简洁的方法可以做到这一点?

我当前的CMake文件:

项目,文件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项目中添加此函数,并填写您希望在主项目中看到的include目录。CMake会照顾剩下的。

项目,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

project (Dependency)
add_library (Dependency SomethingToCompile.cpp)
target_include_directories (Dependency PUBLIC include)

目前还不清楚您想要做什么,以及为什么<code>项目

我对你的第一个想法是

  1. PROJECTsCMakeLists.txt中:

    • Remove add_dependencies(Project Dependency) There is no need to specify dependency, target_link_libraries() already does that.
  2. 依赖sCMakeLists.txt中:

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




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签