6

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?

2
  • 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. See also here. Commented Jun 30, 2016 at 8:30
  • Wow! You're right. You can post that as an answer and I'll accept it. Commented Jun 30, 2016 at 8:37

1 Answer 1

9

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.