0

I want to modify a variable passed to a function. Here's the code I wrote:

FUNCTION(TEST var)
    set(${var} "ABC")
    message(${var}) # 2) 123
    set(${var} "ABC" PARENT_SCOPE)
ENDFUNCTION(TEST)

set(v "123")
message(${v}) # 1) 123
TEST(${v})
message(${v}) # 3) 123

Why all three outputs print 123. I expected #2 and #3 print ABC?

If I pass variable like this - TEST(v) - I have other output: #1 - 123, #2 - v, #3 - ABC. Why is this? What's the difference?

nikitablack
  • 4,359
  • 2
  • 35
  • 68

1 Answers1

1

You are passing the content of v to TEST(). So it should be:

FUNCTION(TEST var)
    set(${var} "ABC")
    message(${${var}})
    set(${var} "ABC" PARENT_SCOPE)
ENDFUNCTION(TEST)

set(v "123")
message(${v}) 
TEST(v)
message(${v}) 

Reference

Florian
  • 39,996
  • 9
  • 133
  • 149
  • Thank you. This works for a "normal" variable but doesn't work for a list variable. And what's this '${${var}}`? Why we need double braces here? – nikitablack May 26 '17 at 07:57
  • @nikitablack That also works for list variables since lists in CMake are only semicolon separated strings. And the double braces are needed for double dereferencing (first the name of the variable, then content of this variable) – Florian May 26 '17 at 19:02