Search code examples
escapingcmake

Why does CMake prefixes spaces with backslashes when executing a command?


I have a custom command in a CMakeLists.txt :

set(testfiles "test1 test2")
add_custom_target(testtouch COMMAND touch ${testfiles})

When I run "make testtouch VERBOSE=1" I see that it executes :

touch test1\ test2

This is just an example, but I have this problem in a real project and the "\ " are breaking the command. Note that I get the variable (here testfiles) from a Find script and can't just get rid of the double quotes.

Why does CMake do that ?

How to avoid it ?


Solution

  • Because sometimes difference between cmake list variable type and cmake string literal become important.

    Try to change the variable setup to the following to avoid the problem:

    set(testfiles "test1" "test2")