I'm trying to enable warnings in a CMake project. Using target_compile_options
similar to what is suggested in this answer works fine:
target_compile_options(mytarget PRIVATE $<$<CXX_COMPILER_ID:GNU,Clang>:-Wall -Wextra -pedantic>)
I have many targets, and I would like to apply these settings to all of them. So, I tried using add_compile_options
instead:
add_compile_options($<$<CXX_COMPILER_ID:GNU,Clang>:-Wall -Wextra -pedantic>)
However, when I use this, I see that the compiler is passed "\$<1:-Wall" -Wextra "-pedantic>"
, as if space-splitting occurred before expanding the generator expression. This confuses the compiler and the build fails.
I tried quoting the argument to add_compile_commands
:
add_compile_options("$<$<CXX_COMPILER_ID:GNU,Clang>:-Wall -Wextra -pedantic>")
But when I do this, the compiler is passed a single "-Wall -Wextra -pedantic"
argument, which also confuses it and causes the build to fail.
I can "distribute" out the generator expressions like this:
add_compile_options(
$<$<CXX_COMPILER_ID:GNU,Clang>:-Wall>
$<$<CXX_COMPILER_ID:GNU,Clang>:-Wextra>
$<$<CXX_COMPILER_ID:GNU,Clang>:-pedantic>
)
This works fine, and solves my immediate problem…
…but, the question remains: what is causing add_compile_options
to work differently from target_compile_options
here? Shouldn't their string-splitting semantics be identical?