CMake list for string: simple semicolon replacement

I am confused with a very simple example. I have a standard list, so basically its string representation uses semicolons as separators. I want to replace it with another one:

set(L1 "A" "B" "C")
message("L1: ${L1}")

string(REPLACE ";" "<->" L2 ${L1})
message("L2: ${L2}")

      

this snippet prints:

L1: A;B;C
L2: ABC

      

and I don't understand why. Consistent with for some other SO answers, my string replacement seems to be valid. What am I doing wrong? Is there a way to store the value A<->B<->C

in my second variable?

Note. I am using CMake 3.7.2

+3


source to share


2 answers


Just put ${L1}

in quotes:

set(L1 "A" "B" "C")
message("L1: ${L1}")

string(REPLACE ";" "<->" L2 "${L1}")
message("L2: ${L2}")

      

Otherwise, the list will be expanded again into a list of parameters, separated by spaces.



Link

+5


source


But ${L1}

it is not a string, it is a list. If you want a string, you need to enclose it in double quotes, for example "${L1}"

.



+4


source







All Articles