Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I correctly pass CMake list (semicolon-sep) of flags to set_target_properties?

Tags:

cmake

CMake lists are essentially just semicolon-separated strings, but if you pass such a variable to a command, it does get expanded into multiple arguments - for example,

set(FLAGS f1 f2 f3) # now FLAGS == 'f1;f2;f3' add_custom_command(   ...   COMMAND my_cmd ${FLAGS}   ... ) 

will correctly call my_cmd f1 f2 f3.

Now if I do this with

set_target_properties(   myTarget PROPERTIES   LINK_FLAGS  "${LD_FLAGS}" ) 

the expansion does not occur, and I end up with a single LD_FLAG that contains semicolons -- useless, instead of expanding it into a space-separated string.

Is there any way to make it so that when I pass a list to the LINK_FLAGS property (or any property that is), it gets expanded into multiple arguments rather than just one?

Thanks, Dan

like image 390
Dan Avatar asked Jul 21 '12 18:07

Dan


People also ask

How do I use a list in CMake?

A list in cmake is a ; separated group of strings. To create a list the set command can be used. For example, set(var a b c d e) creates a list with a;b;c;d;e , and set(var "a b c d e") creates a string or a list with one item in it.

Where is CMake list?

Editing CMakeLists Files These can be found in the Auxiliary directory of the source distribution, or downloaded from the CMake Download page.


2 Answers

I don't think set_target_properties can do the expansion automatically, but you can use string (REPLACE ...) to expand a list into a space separated string:

string (REPLACE ";" " " LD_FLAGS_STR "${LD_FLAGS}") set_target_properties(   myTarget PROPERTIES   LINK_FLAGS  "${LD_FLAGS_STR}" ) 
like image 167
sakra Avatar answered Sep 19 '22 23:09

sakra


For using a cmake List as list, use

${LD_FLAG} 

For using a cmake list as string, (i.e. list items are separated with ';'), use

"${LD_FLAG}" 

So in your case, just remove "" should be sufficient.

like image 34
Ding-Yi Chen Avatar answered Sep 18 '22 23:09

Ding-Yi Chen