c++cmakegenerator-expression

Cmake Generator expressions with multiple space separated entries


I'm writing a C++ project and use Cmake for it.

In my Cmake file I have:

set(MY_DEBUG_WARNINGS "-Og -Wall"
)

# Add warnings:
add_compile_options("$<$<CONFIG:DEBUG>:${MY_DEBUG_WARNINGS}>"
"$<$<CXX_COMPILER_ID:Clang,AppleClang>:-fcolor-diagnostics>"
"$<$<CXX_COMPILER_ID:GNU>:-fdiagnostics-color>"
)

which doesn't work, because the compile command picks up extra escaped quotes:

  "/usr/bin/clang++ -I../src/Exception/include -g \"-Og -Wall\" -fcolor-diagnostics - 
  std=c++17 -o src/Exception/CMakeFiles/exception.dir/Exception.cpp.o -c 
  /home/bob/app/src/Exception/Exception.cpp",
  "file": "/home/bob/app/src/Exception/Exception.cpp"  

The problem seems to be the space in the warnings.

This question "solves" the issue by wrapping each value in its own expression, which seems weird and awfully impractical. Is there any easier way to pass a string with spaces in it in a generator expression?


Solution

  • "-Og -Wall" is a string, whereas "-Og;-Wall" is a list (as lists are semicolon separated strings).

    So set(my_list -Og -Wall) (no double quotes) also creates a list. Read all about it here.