问题
(During writing this question I found a solution, so this just documents it, because I would really have needed it beforehand!)
Im 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?
As mentioned I found an acceptable solution myself but will not yet mark it as my accepted answer because I'm not sure, this is a good solution and not a hack that may bite me if I ever need semi-colons in the options themselves.
回答1:
"-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.
回答2:
The problem is indeed the space and it can be solved like this:
set(MY_DEBUG_WARNINGS "-Og;-Wall"
)
replacing all spaces with a semi-colon. With this the compile command becomes:
"/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",
which actually works.
I have no idea whether this makes problems elsewhere, so a comment from someone with more knowledge would be much appreciated.
来源:https://stackoverflow.com/questions/65035750/cmake-generator-expressions-with-multiple-entries-spaces