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

Viewed 34654

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?

6 Answers
separate_arguments(LD_FLAGS_AS_LIST  NATIVE_COMMAND  LD_FLAGS)

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

In cmake 3.x there are a few approaches

The top level CMakeLists.txt file then looks something like this:

# The name of the included file could be anything,
# it doesn't have to be called CMakeLists.txt
include(foo/CMakeLists.txt)
include(bar/CMakeLists.txt)

add_executable(myApp ${myApp_SOURCES})

with the subdirectory files structured something like this:

list(APPEND myApp_SOURCES
    ${CMAKE_CURRENT_LIST_DIR}/foo.cpp
    ${CMAKE_CURRENT_LIST_DIR}/foo_p.cpp
)

Enhanced source file handling with target_sources()

Related