Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

escaping

cmake

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 ?

like image 724
Barth Avatar asked Jan 19 '12 11:01

Barth


People also ask

How do I change my path in CMake?

CMake will use whatever path the running CMake executable is in. Furthermore, it may get confused if you switch paths between runs without clearing the cache. So what you have to do is simply instead of running cmake <path_to_src> from the command line, run ~/usr/cmake-path/bin/cmake <path_to_src> .

What is ${} in CMake?

You access a variable by using ${} , such as ${MY_VARIABLE} . 1. CMake has the concept of scope; you can access the value of the variable after you set it as long as you are in the same scope. If you leave a function or a file in a sub directory, the variable will no longer be defined.

How do I set CMake cache entry?

You can use the command line to set entries in the Cache with the syntax cmake -D var:type=value , just cmake -D var=value or with cmake -C CMakeInitialCache. cmake . You can unset entries in the Cache with unset(... CACHE) .

Is CMake case sensitive?

CMake variable names are case sensitive and may only contain alphanumeric characters and underscores.


2 Answers

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")
like image 122
Andrey Kamaev Avatar answered Sep 21 '22 17:09

Andrey Kamaev


I was trying something similar with multiple arguments some are strings and some are not, i.e.:

add_custom_target(externalProject COMMAND make -s VALUE1:="${argument1}" VALUE2:=${argument2} VALUE3:="${argument3}" ...)

Where argument3 contained "VAL1 VAL2 VAL3".

I found that replacing the spaces with semicolons ; resolved the issue for me when cmake creates the makefile.

like image 45
Daren Hayward Avatar answered Sep 21 '22 17:09

Daren Hayward