[CMake] Functions inherit parent variables?

Robert Dailey rcdailey at gmail.com
Thu Mar 1 12:01:35 EST 2012


No, the print statement is not missing. In fact it prints just fine
(function test() is able to obtain the value for variable SOME_TEST).

This isn't exactly the same as C++. In C++, a function does not have access
to the calling function's local declarations. In order for the function to
get access to these, they must be passed in as parameters.

---------
Robert Dailey


On Wed, Feb 29, 2012 at 9:54 PM, Michael Hertling <mhertling at online.de>wrote:

> On 03/01/2012 01:38 AM, Robert Dailey wrote:
> > I ran a quick test:
> >
> >
> > function( test )
> > message( "SOME_TEST: ${SOME_TEST}" )
> > endfunction()
> >
> > function( start )
> > set( SOME_TEST "HELLO WORLD" )
> > test()
> > endfunction()
> >
> > start()
> >
> >
> > Seems like a function has access to the calling scope's defined
> variables.
> > I thought because functions created a new scope, that excluded access to
> > variables defined in the outer scope (i.e. calling scope)
> >
> > Can someone explain?
>
> The line "SOME_TEST: HELLO WORLD" is missing, I guess?
>
> As usual with scoping mechanisms, there is access to the outer scope
> from within the inner scope: Read access via ordinary dereferencing
> and write access via PARENT_SCOPE. It's quite the same as in C/C++
> with the { and } tokens; see also the C++ "::" operator.
>
> Regards,
>
> Michael
> --
>
> Powered by www.kitware.com
>
> Visit other Kitware open-source projects at
> http://www.kitware.com/opensource/opensource.html
>
> Please keep messages on-topic and check the CMake FAQ at:
> http://www.cmake.org/Wiki/CMake_FAQ
>
> Follow this link to subscribe/unsubscribe:
> http://www.cmake.org/mailman/listinfo/cmake
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.cmake.org/pipermail/cmake/attachments/20120301/8c72fa8e/attachment.htm>


More information about the CMake mailing list