0
cmake_minimum_required(VERSION 3.9)
project(test)


set(MY_VAR "XXXXXX")

function(echo var)
    message(STATUS "var = ${var}")
endfunction()


echo(MY_VAR) # output MY_VAR     

why the output is MY_VAR, it should be XXXXXX, is cmake function parameter doesn't pass by value/reference but literal?

it seems that vairable in cmake function call is treated as string, it is a so wired design. when we need to read and modify a variable in function, we have to wirte in this ugly way?

set(MY_VAR "XXXXXX")
function(echo var)
    message(STATUS "var = ${${var}}")
    set(${var} "new value")
endfunction()
echo(MY_VAR)
woder
  • 647
  • 5
  • 12
  • Because you literally pass `MY_VAR` (the string) to the function. Try `echo($MY_VAR)`. – tkausl Apr 19 '21 at 00:12
  • @tkausl thanks, but vairable in function call is treated as string is a so wired design. when we need to read and modify a variable in function, we have to wirte in this ugly way? **``` set(MY_VAR "XXXXXX") function(echo var) message(STATUS "var = ${${var}}") set(${var} "new value") endfunction() echo(MY_VAR) # output MY_VAR ```** – woder Apr 19 '21 at 00:21
  • Well, this is how CMake works. – arrowd Apr 19 '21 at 06:31
  • If you are looking for the way of returning value from a function via its parameter, then see [that question](https://stackoverflow.com/questions/22487215/return-a-list-from-the-function-using-out-parameter). Otherwise I don't understand the question. – Tsyvarev Apr 19 '21 at 07:29

0 Answers0