1

What is the best practice to modify a caller's variable from inside a CMake-function. Assume

function(MyFunction IN_OUT_NAME)

   ... what to do here ...

   string(APPEND ${IN_OUT_NAME} " and that")

   ... what to do here ...

endfunction()

What needs to be done such that the following code fragment

set(MY_MESSAGE "this")
MyFunction(MY_MESSAGE)
message(${MY_MESSAGE})

delivers

this and that

Not-a-duplicate-remarks:

Frank-Rene Schäfer
  • 3,182
  • 27
  • 51

1 Answers1

2

Just use PARENT_SCOPE to export the value to parent scope.

function(MyFunction IN_OUT_NAME)
   string(APPEND ${IN_OUT_NAME} " and that")
   set(${IN_OUT_NAME} "${${IN_OUT_NAME}}" PARENT_SCOPE)
endfunction()

set(MY_MESSAGE "this")
MyFunction(MY_MESSAGE)
message(${MY_MESSAGE})
KamilCuk
  • 120,984
  • 8
  • 59
  • 111