How to use add_custom_command right?

Hi there,
I am quite new to CMake and can see some weird benaviors. Also I could see similar issues of people to this kind of problem but could not see a good solution.
Hopefully, someone can help.
Suppose I use add_custom_command in the following way:

set(flags “-c -t -o”)
add_custom_command(COMMAND ${CMAKE_C_COMPILER} ${flags} /EP xx.c > xx.i )

On windows this will generate the following:

“\cl.exe” “-c -t -o” /EP xx.c > xx.i

when u build with microsoft VS you get an error saying it can’t open file “-c -t -o”.
This is due to the fact that cmake puts those quotes around the flags string variable.
The question is how do I get rid of those quotes and what is the right way to use this command.

Remark: you could say: do not use quotes when you define flags but in real flow I get flags via get_property() which seems to have those quotes in there.

Thanks
Serge

add_custom_command takes strings as is without any interpretation.

To expect correct syntax, specify the flags as a list not a string. And to avoid problems if the path of the compiler contains some spaces, use quotes:

set(flags -c -t -o)
add_custom_command(COMMAND "${CMAKE_C_COMPILER}" ${flags} /EP xx.c > xx.i )
1 Like

Thank you Marc,
and it solved the problem.
Awesome, also a little bit counter intuitive for someone used to C/C++ python and other programming languages.

Cheers.