Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I correctly create dependencies between targets in CMake?

Tags:

c++

build

cmake

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.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.

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, file 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:

project(Dependency)
add_library(Dependency SomethingToCompile.cpp)
target_link_libraries(Dependency)
like image 465
radman Avatar asked Feb 05 '11 04:02

radman


People also ask

How do I specify dependencies to CMake?

New in version 3.3: Allow adding dependencies to interface libraries. See the DEPENDS option of add_custom_target() and add_custom_command() commands for adding file-level dependencies in custom rules. See the OBJECT_DEPENDS source file property to add file-level dependencies to object files.

What are CMake dependencies?

CMake files provided with a software package contain instructions for finding each build dependency. Some build dependencies are optional in that the build may succeed with a different feature set if the dependency is missing, and some dependencies are required.

How do I add a target in CMake?

Use the add_custom_command() command to generate a file with dependencies. By default nothing depends on the custom target. Use the add_dependencies() command to add dependencies to or from other targets.

Can CMake install dependencies?

CMake itself does not allow to install dependencies automatically.


1 Answers

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) 
like image 50
dipp Avatar answered Oct 05 '22 01:10

dipp