John Zwinck
John Zwinck

Reputation: 249444

Nested function calls in CMake (use one's result as another's argument)

Test CMakeLists.txt:

function(foo param)
    return("hello")
endfunction()

message(foo("oops"))

When "cmake ." is run with the above file, it prints "foo(oops)". I expected it to print "hello".

How can I make one function receive the result of another in CMake?

Upvotes: 0

Views: 1764

Answers (1)

Daniel Schepler
Daniel Schepler

Reputation: 3103

CMake functions don't "return values" in the way the code example you gave thinks they do. In general, if a CMake builtin or function wants to return a value, it needs to take the name of a variable to store the result in:

function(foo param ret)
    set(${ret} "hello ${param}" PARENT_SCOPE)
endfunction()

Then, you need to construct the control flow in the caller yourself:

foo("world" hello_world)
message(STATUS "foo returned: ${hello_world}")
foo(${hello_world} hello2)
message(STATUS "foo second call returned: ${hello2}")

Upvotes: 1

Related Questions