Cmake list append for compiler flags giving dummy results?

I need to add various flags to my C and C ++ compilations in my CMake files (CMake 2.8.10.2). I see that some people use add_definitions , but from what I see, they are for preprocessor flags ( -D ). I have several flags that I do not want to pass to the preprocessor.

So, I tried to change CMAKE_C_FLAGS and CMAKE_CXX_FLAGS . I see that some people used 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 correct way to do this is to use list(APPEND ...) , for example:

 list(APPEND CMAKE_C_FLAGS -new -flags -here) 

However, when I do this, my compilation line contains flags separated by a semicolon, and is a syntax error. I read that lists are now stored internally, but I decided that cmake would consider this when I used the variable. It seems so basic; Am I doing something wrong? I mean, how good are these lists, if they cannot be used, if you don’t need a list of values ​​separated by a semicolon (and who wants this, except that I think that Windows% PATH% settings or something yet)? Should I use the cited version, even if the documents offer less efficient / relevant?

+6
source share
2 answers

In this case, you would usually use the set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -new -flags -here") technique set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -new -flags -here") .

You are correct that in most other contexts, CMake can "translate" a colon-separated list to something meaningful to the compiler (for example, a list of source files in an executable file), but in this case CMake accepts flags as a single, complete line for transmission compiler / linker.

If you really want to save the flag list as a CMake list, but then before you exit CMakeLists.txt, you can translate the list yourself into a single string value CMAKE_C_FLAGS , but this is unusual for see this.

+6
source

In CMake, a “list” is a string of items separated by a half-colon symbol. For instance:

 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- segmented elements is just a string, not a list. Use the string(APPEND) command to add to it. For instance:

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

In older versions of CMake that lack the string(APPEND) command, you should return to the set command. For instance:

 set(FOO "a") set(FOO "${FOO} b") set(FOO "${FOO} c") 
+1
source

Source: https://habr.com/ru/post/944498/


All Articles