Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Avoid quotes when using CMake to pass a process output as compile options for specific target

Tags:

quotes

cmake

I have a problem with CMake: I use execute_process() to set a variable and I want that variable to pass it as options to the compiler. CMake sets quotes around the variable so that the compiler gets wrong input.

Concretely: I need to compile SDL2 with only a specific target.

# CMakeLists.txt
execute_process(COMMAND "sdl2-config" "--cflags" OUTPUT_VARIABLE SDL2_CFLAGS OUTPUT_STRIP_TRAILING_WHITESPACE)
target_compile_options(SpecialTarget PUBLIC ${SDL2_CFLAGS})

The output of sdl2-config --cflags is:

-I/usr/include/SDL2 -D_REENTRANT

CMake calls the compiler now in this way:

/usr/bin/c++ ... "-I/usr/include/SDL2 -D_REENTRANT" ...

Of course this does not work. I need to get rid of the quotes.

If a use

add_definitions(${SDL2_CFLAGS})

everything works. But I need target_compile_options because I want the options not on all targets.

like image 955
theHacker Avatar asked Dec 25 '14 21:12

theHacker


1 Answers

You can use the separate_arguments command:

separate_arguments(SDL2_CFLAGS UNIX_COMMAND "${SDL2_CFLAGS}")
target_compile_options(SpecialTarget PUBLIC ${SDL2_CFLAGS})
like image 78
Fraser Avatar answered Nov 07 '22 10:11

Fraser