Reputation: 3078
So let's say we have a CMake function called func
which takes named arguments, say NAMED1
and NAMED2
. The first argument is mandatory and the second is optional. For example
func(NAMED1 foo NAMED2 optional)
Now I have a loop which needs to call this function func
at each iteration, but depending on the current iteration I sometimes need to supply the optional value and sometimes not. So at each iteration I build a list of the arguments, say
list(APPEND args "NAMED1" "foo")
if (...something...)
list(APPEND args "NAMED2" "optional")
endif()
So I have the args
list now. As a string, it may look like
NAMED1;foo
or it may look like
NAMED1;foo;NAMED2;optional
So I thought to string replace the whole list with
string(REPLACE ";" " " args "${args}")
Then the resulting args
will look like
NAMED1 foo NAMED2 optional
So I thought to pass this string to the function func
, with
func("${args}")
But the problem now is that CMake thinks this is one big string, while it should interpret it as separate strings. How can I achieve this?
Upvotes: 6
Views: 6800
Reputation: 42852
Turning my comment into an answer
Just func(${args})
without the quotes and without the need of replacing the ;
should work.
I've been using this a lot of times in the past (especially with forwarding "the rest of the parameters" with ${ARGN}
).
References
Upvotes: 9