Avoid quotes when using CMake to pass the output of a process as compilation options for a specific target
I have a problem with CMake: I am using execute_process()
to set a variable and I want this variable to pass it as parameters to the compiler. CMake sets quotes around the variable so that the compiler receives incorrect input.
To be specific: I only need to compile SDL2 for a specific purpose.
# CMakeLists.txt
execute_process(COMMAND "sdl2-config" "--cflags" OUTPUT_VARIABLE SDL2_CFLAGS OUTPUT_STRIP_TRAILING_WHITESPACE)
target_compile_options(SpecialTarget PUBLIC ${SDL2_CFLAGS})
Conclusion sdl2-config --cflags
:
-I/usr/include/SDL2 -D_REENTRANT
CMake now invokes the compiler like this:
/usr/bin/c++ ... "-I/usr/include/SDL2 -D_REENTRANT" ...
Of course it doesn't work. I need to get rid of quotes.
If using
add_definitions(${SDL2_CFLAGS})
everything is working. But I need it target_compile_options
because I want the parameters to be not for all purposes.
source to share
You can use separate_arguments
:
separate_arguments(SDL2_CFLAGS UNIX_COMMAND "${SDL2_CFLAGS}")
target_compile_options(SpecialTarget PUBLIC ${SDL2_CFLAGS})
source to share