[CMake] List from strings (CMAKE_CXX_FLAGS and friends)

James Bigler jamesbigler at gmail.com
Tue Nov 27 15:12:56 EST 2012


I'm running into this again, and I trawled through the mailing list, and
according to Bill (4/11/2009 - managing lists with space separated
elements), I should be able to get a list from a string with a single
command.

If you want to convert a string to a list you can do it like this:

set(list ${string})

That will make the space separated list string into a ; separated list.  If
you want to keep string a string you need quotes:
set(newstring "${string}").

It doesn't seem to work that way though, so perhaps I'm missing something.

Here's my test program:

function(print_list name)
  list(LENGTH ${name} length_of_list)
  message("${name} has ${length_of_list} items")
  set(count 0)
  foreach(i ${${name}})
    message("${name}[${count}] = ${i}")
    math(EXPR count "${count} + 1")
  endforeach()
endfunction()

set(mylist a b c)
set(mystring "a b c")
set(mylist_from_string ${mystring})

print_list(mylist)
print_list(mystring)
print_list(mylist_from_string)

And here's what I get when I run it:

cmake -P list-from-string.cmake

mylist has 3 items
mylist[0] = a
mylist[1] = b
mylist[2] = c
mystring has 1 items
mystring[0] = a b c
mylist_from_string has 1 items
mylist_from_string[0] = a b c
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.cmake.org/pipermail/cmake/attachments/20121127/f0a4ed96/attachment.htm>


More information about the CMake mailing list