1

I have a CMakeLists.txt which builds a number of targets. Call one foo and one bar

At the moment foo and bar both share some settings given to ccmake configuration

 CMAKE_CXX_FLAGS =  -W -Wall
 CMAKE_CXX_FLAGS_DEBUG = -g -pg

etc

I need to add -fPIC to foo but not bar. According to this answer I want to use TARGET_COMPILE_OTIONS

target_compile_options(foo PUBLIC "$<$<CONFIG:DEBUG>:${MY_DEBUG_OPTIONS}>")
target_compile_options(foo PUBLIC "$<$<CONFIG:RELEASE>:${MY_RELEASE_OPTIONS}>")

Note that target_compile_options add [sic] options

This sounds like it's what I need but what does this syntax mean?

"$<$<CONFIG:DEBUG>:${MY_DEBUG_OPTIONS}>"

To clarify, I want to add -fPIC as an additional flag when compiling foo but not when compiling bar

Please explain the $<$< business and show me, concretely, how -fPIC would be added as a flag for foo.

Community
  • 1
  • 1
spraff
  • 32,570
  • 22
  • 121
  • 229

1 Answers1

2

Looks like $<$< falls into the generator expressions category: https://cmake.org/cmake/help/v3.0/manual/cmake-generator-expressions.7.html#manual:cmake-generator-expressions(7), precisely into

logical expressions

So in your case,

"$<$<CONFIG:DEBUG>:${MY_DEBUG_OPTIONS}>"

expands to MY_DEBUG_OPTIONS when the DEBUG configuration is used, and otherwise expands to nothing. So in your case you should add -fPIC for example to MY_DEBUG_OPTIONS. To be a little bit more precise:

$<CONFIG:DEBUG>

evaluates to 1 or 0 depending weather CONFIG is DEBUG or not, respectively. Then you will have either:

$<0:${MY_DEBUG_OPTIONS}>

or

$<1:${MY_DEBUG_OPTIONS}>

The two expressions above will evaluate in the following way:

$<0:${MY_DEBUG_OPTIONS}> will evaluate to

Empty string (ignores ${MY_DEBUG_OPTIONS})

while $<1:${MY_DEBUG_OPTIONS}> will evaluate to

Content of ${MY_DEBUG_OPTIONS}

as the documentation states.
In the last case then -fPIC will be added to one of CMAKE_CXX_FLAGS or CMAKE_CXX_FLAGS_DEBUG.

fedepad
  • 4,509
  • 1
  • 13
  • 27