CMake的如何正确地创建目标之间的依赖关系 [英] CMake how to correctly create dependencies between targets

查看:12155
本文介绍了CMake的如何正确地创建目标之间的依赖关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用的CMake建立一个C ++项目,它使用的库之间的一些简单的依赖关系。

的设置如下:


  • 项目

    • 依赖


项目本身包含的源文件,其中包括从依附头和可执行文件时建造它需要对依赖的静态库链接。

到目前为止,我能得到这个工作,但我有手动项目的CMakeLists文件来指定依赖的include目录。我想这自动复杂得多拉出来,我已经探讨使用find_package(的选择)命令有限的成功,这样做,使事情。

所有我想要做的是有相关性项目前建成并具备对库项目链接,并拥有包括目录,是否有实现这一目标的一个简单简洁的方式?

我目前cmake的文件:

项目,的CMakeLists.txt:

  cmake_minimum_required(版本2.6)
项目(项目)
include_directories($ {} PROJECT_SOURCE_DIR /项目)
add_subdirectory(依赖)
add_executable(项目main.cpp中)
target_link_libraries(项目依赖)
add_dependencies(项目依赖)

依存关系的CMakeLists.txt

 项目(依赖)
add_library(依赖SomethingToCompile.cpp)
target_link_libraries(依赖)


解决方案

由于 CMake的2.8.11 您可以使用 target_include_directories 。只需简单地在你的依赖项目中添加这一功能,并填写包括要在主体工程看目录。 CMake的会照顾的其余部分。

项目,的CMakeLists.txt:

  cmake_minimum_required(VERSION 2.8.11)
项目(项目)
include_directories(项目)
add_subdirectory(依赖)
add_executable(项目main.cpp中)
target_link_libraries(项目依赖)

依存关系的CMakeLists.txt

 项目(依赖)
add_library(依赖SomethingToCompile.cpp)
target_include_directories(PUBLIC依赖包括)

I am trying to use CMake to set up some simple dependencies between a C++ project and the libraries that it uses.

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.

So far I can get this to work but I have to specify the include directories of Dependency in the CMakeLists 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.

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?

My current cmake files:

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, CMakeLists.txt

project(Dependency)
add_library(Dependency SomethingToCompile.cpp)
target_link_libraries(Dependency)

解决方案

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.

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

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

这篇关于CMake的如何正确地创建目标之间的依赖关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆