问题描述
如标题中所述,我想在cmake中编写一个 nice函数,该函数能够修改作为参数传递给该函数的变量。
as in said in the title, I would like to write a "nice" function in cmake that is able to modify a variable which is passed as a parameter into that function.
我能想到的唯一方法是丑陋的:
The only way I can think of doing it is ugly:
函数定义
function(twice varValue varName)
set(${varName} ${varValue}${varValue} PARENT_SCOPE)
endfunction(twice)
用法
set(arg foo)
twice(${arg} arg)
message("arg = "${arg})
结果
arg = foofoo
在我看来,没有任何变量可以传递的真实概念?!
我觉得我尚未接受cmake的一些基本知识。
It seems to me, there is no real concept of variables that one can pass around at all?!I feel like there is something fundamental about cmake that I didn't take in yet.
那么,有没有更好的方法呢?
So, is there a nicer way to do this?
非常感谢!
推荐答案
您不需要传递值和变量名。名称就足够了,因为您可以通过名称访问值:
You don't need to pass the value and the name of the variable. The name is enough, because you can access the value by the name:
function(twice varName)
SET(${varName} ${${varName}}${${varName}} PARENT_SCOPE)
endfunction()
SET(arg "foo")
twice(arg)
MESSAGE(STATUS ${arg})
输出 foofoo
这篇关于如何编写一个通过引用传递变量的漂亮函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!