[CMake] How to export the user-defined variable in to the parent directory?

Alexander Neundorf a.neundorf-work at gmx.net
Wed Jul 25 16:01:50 EDT 2012


On Saturday 14 July 2012, 黄光成 wrote:
> my directory hierarchical is : dir1/dir2/dir3.
> In dir3's CMakeLists.txt, I define a custom variable named 'xxx' using the
> command set(xxx yyy). I want to use the variable 'xxx' in dir1's
> CMakeLists.txt, how to do that? Can export the user-defined variable?

For getting a variable one level up, using PARENT_SCOPE should work:

set(foo "abc" PARENT_SCOPE)

Calling this in dir3 directly in the CMakeLists.txt (i.e. not in a function) 
would set foo in dir2.

Alternatively, you can set a variable globally, via two ways:

set(foo "abc" CACHE STRING "something..." FORCE)

or using a GLOBAL property:

in dir1/dir2/dir3/:
set_property(GLOBAL .... )

and then access it from anywhere else

get_property(GLOBAL ....)

I think setting a directory property should also work:

in dir1/dir2/dir3/:
set_property(DIRECTORY PROPERTY foo "abc")

then accessing it should also work from everywhere I think:

get_property(myFoo DIRECTORY ${CMAKE_SOURCE_DIR}/dir1/dir2/dir3 PROPERTY foo)

Alex
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.cmake.org/pipermail/cmake/attachments/20120725/b56dee20/attachment-0001.htm>


More information about the CMake mailing list