0

Hey guys I read that in modern Cmake we should avoid add_compile_definitions and use target_compile_definitions instead

https://gist.github.com/mbinna/c61dbb39bca0e4fb7d1f73b0d66a4fd1#forget-the-commands-add_compile_options-include_directories-link_directories-link_libraries

at the moment I'm using add_compile_definitions() in my root CmakeLists

if()
add_compile_definitions(SOME_TARGET_A_SPECIFIC_DEFINE)
else if()
add_compile_definitions(SOME_TARGET_B_SPECIFIC_DEFINE)
endif()

Now I'm trying to replace this with target_compile_definitions. My project structure looks like this

  • CMakeLists.txt
  • app
  • app/TargetA/CMakeLists.txt
  • app/TargetB/CMakeLists.txt
  • modules
  • modules/SomeModule/CMakeLists.txt

Now when i move add_compile_definitions(SOME_TARGET_A_SPECIFIC_DEFINE) from root to app/TargetA/CMakeLists.txt my Module (which depends on SOME_TARGET_A_SPECIFIC_DEFINE) the module will not build. From my understanding if would have to add add_compile_definitions(SOME_TARGET_A_SPECIFIC_DEFINE) to each module like this

add_library("${module_name}"
            ${MODULE_SOURCES}) 
target_link_libraries("${module_name}" ${MyLibs})
if()
  target_compile_definitions("${module_name}" SOME_TARGET_A_SPECIFIC_DEFINE)
else if()
  target_compile_definitions("${module_name}" SOME_TARGET_B_SPECIFIC_DEFINE)
endif()

So my Point here to summarise: if I use add_compile_defintions() in the root folder I'd only have to map the proper defines once. If would like to move to target_compile_definitions I'd have to add this the EVERY module?

Or is there a better way to pass Target Specific defines to multiple libs?

JHeni
  • 455
  • 3
  • 12
  • 2
    Does this answer your question? [target\_compile\_definitions for multiple CMake targets?](https://stackoverflow.com/questions/47611319/target-compile-definitions-for-multiple-cmake-targets) – SamBob Oct 01 '21 at 13:59
  • 1
    if you add `SOME_TARGET_A_SPECIFIC_DEFINE` as a public define to target A then everything else that links to target A will inherit the same define – Alan Birtles Oct 01 '21 at 14:00

0 Answers0