5

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?

rwols
  • 2,968
  • 2
  • 19
  • 26
  • 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](http://stackoverflow.com/questions/35847655/cmake-when-to-quote-variables). – Florian Jun 30 '16 at 08:30
  • Wow! You're right. You can post that as an answer and I'll accept it. – rwols Jun 30 '16 at 08:37

1 Answers1

8

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

Community
  • 1
  • 1
Florian
  • 39,996
  • 9
  • 133
  • 149