Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cmake list append for compiler flags yields bogus results?

Tags:

cmake

I need to add various flags to my C and C++ compile lines in my CMake files (CMake 2.8.10.2). I see some people use add_definitions but from what I can see that is intended for preprocessor flags (-D). I have some flags that I don't want passed to the preprocessor.

So I've been trying to modify CMAKE_C_FLAGS and CMAKE_CXX_FLAGS. I see that some people were using something like:

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -new -flags -here")

but then I read in the cmake docs that this is less efficient, and the right way to do it is to use list(APPEND ...), like this:

list(APPEND CMAKE_C_FLAGS -new -flags -here)

However, when I do this my compile line contains the flags separated by semicolons and is a syntax error. I read that this is now lists are stored internally, but I figured this would be taken care of by cmake when I used the variable. This seems so basic; am I doing something wrong? I mean, what the heck good are these lists if they can't be used unless you happen to want a semicolon-separated list of values (and who wants that, other than I guess Windows %PATH% settings or something)? Should I be using the quoted version even though the docs suggest it's less efficient/appropriate?

like image 992
MadScientist Avatar asked May 08 '13 05:05

MadScientist


1 Answers

In CMake, a "list" is a string of items separated by semi-colons. For example:

set(FOO "a")
list(APPEND FOO "b") # now FOO="a;b"
list(APPEND FOO "c") # now FOO="a;b;c"

In CMake, a string of space-seperated items is just a string, not a list. Use the string(APPEND) command to append to it. For example:

set(FOO "a")
string(APPEND FOO " b") # now FOO="a b"
string(APPEND FOO " c") # now FOO="a b c"

On old versions of CMake that lack the string(APPEND) command, you should fallback to the set command. For example:

set(FOO "a")
set(FOO "${FOO} b")
set(FOO "${FOO} c")
like image 158
Chadversary Avatar answered Sep 22 '22 07:09

Chadversary