flatline
flatline

Reputation: 42613

CMake add_definitions not scoped to current directory

The documentation for add_definitions states that:

Adds definitions to the compiler command line for sources in the current directory and below

But when I use it - doesn't matter where I put it - the definition gets added to every target across the entire project and prompts a full rebuild. In the CMakeLists.txt for the target of interest I'm adding:

if (MY_OPTION)
  add_definitions(-DMY_DEFINITION)
endif()

And -DMY_DEFINITION shows up in the ninja.build file for every target across the entire project. I've tried moving this block of code up and down in the folder hierarchy to no avail. I'm still on CMake 2.8.12 if that makes a difference, but the documentation there is the same. Am I misusing this in some way, or is this the intended behavior?

Edit: Some details about the project structure in response to question from @Tsyvarev:

3rdParty/
   Dep1/
     CMakeLists.txt
   ...
CustomDep1/
  CMakeLists.txt
MyProject/
  Project.cmake
  CMakeLists.txt
  MyTarget1/
    CMakeLists.txt

The option is specified in Project.cmake. Definition is only required by MyTarget1. I've tried to put add_dependencies in MyProject/CMakeLists.txt, and in MyTarget1/CMakeLists.txt. It propagates to everything in 3rdParty, CustomDep1, etc.

Upvotes: 3

Views: 5780

Answers (1)

Serhii Koltsiuk
Serhii Koltsiuk

Reputation: 54

See this post and use COMPILE_DEFINITIONS for fine tuning of target or specific sources.

Also add_definitions works in current directory and below and current directory is a directory where lying CMakeLists.txt which include Project.cmake.

Upvotes: 3

Related Questions