[CMake] Parameters of functions

Michael Wild themiwi at gmail.com
Thu May 12 11:10:41 EDT 2011

I agree, that this behaviour is due the fact that the parameter name
"var" hides the parent-scope variable "var", but then ${${var}} should
IMHO result in an error or warning message. Essentially, what this does
inside the function is

set(var var)

which is hardly ever what is intended. This could be fairly easily
detected and warned about.

There should be a way to safely reference parent-scope variables, e.g.


On 05/12/2011 05:00 PM, Glenn Coombs wrote:
> If you really wanted to pass the name of the variable in rather than the
> value, then as Rolf says the behaviour you get is as expected.  The only way
> round that would be to rename var in the function to something guaranteed
> not to be the name of a variable that you ever tried to pass in.
> Or you could use a macro instead:
> MACRO(buildm var)
>        MESSAGE(STATUS "var: " ${${var}})
> SET(var red blue yellow green)
> buildm(var)
> -- var: redblueyellowgreen
> --
> Glenn
> On 12 May 2011 13:21, Micha Renner <Micha.Renner at t-online.de> wrote:
>> Am Donnerstag, den 12.05.2011, 11:50 +0100 schrieb Glenn Coombs:
>>> I think you probably wanted to write and call your function like this:
>>> FUNCTION(build var)
>>>        MESSAGE(STATUS "var: ${var}")
>>> ENDFUNCTION(build)
>>> SET(var red blue yellow green)
>>> build("${var}")
>>> That prints out as you would expect:
>>> -- var: red;blue;yellow;green
>> Both versions are possible.
>> My point was, that in the case I described, the name of the parameter of
>> the function may not be the same as the name of variable the function is
>> called.
>> With your version you don't have this problem.
>> Thanks.
>> Micha

More information about the CMake mailing list