CMake:如何从函数内部修改变量



从CMake函数内部修改调用方变量的最佳实践是什么。假设

function(MyFunction IN_OUT_NAME)
... what to do here ...
string(APPEND ${IN_OUT_NAME} " and that")
... what to do here ...
endfunction()

需要做什么这样的代码片段

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

提供

this and that

无重复标记:

  • 修改函数中的变量是关于JavaScript而不是CMake

  • JavaScript是一种传递引用语言还是传递值语言?是关于JavaScript而不是CMake

只需使用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})

最新更新