CMake: Output a list with delimiters

You could write a function to join the items of a list together with a delimiter, and then print that out instead. For example, such a function:

function (ListToString result delim)
    list(GET ARGV 2 temp)
    math(EXPR N "${ARGC}-1")
    foreach(IDX RANGE 3 ${N})
        list(GET ARGV ${IDX} STR)
        set(temp "${temp}${delim}${STR}")
    endforeach()
    set(${result} "${temp}" PARENT_SCOPE)
endfunction(ListToString)

Then, you could use it like so:

set(my_list a b c d)
ListToString(str ", " ${my_list})
message(STATUS "${str}")

Which outputs:

a, b, c, d

Enclose the dereferenced variable in quotes.

set(my_list a b c d)
message("${my_list}")

Outputs

a;b;c;d

Tags:

Cmake