Consider the following minimal example:
. βββ bar β βββ CMakeLists.txt βββ CMakeLists.txt
where ./CMakeLists.txt is
project( foo ) cmake_minimum_required( VERSION 2.8 ) set( FOO "Exists in both, parent AND in child scope." ) add_subdirectory( bar ) message( STATUS "Variable BAR in ./ = ${BAR}" ) message( STATUS "Variable FOO in ./ = ${FOO}" )
and ./bar/CMakeLists.txt is
set( BAR "Exists in parent scope only." PARENT_SCOPE ) message( STATUS "Variable BAR in ./bar/ = ${BAR}" )
The relevant part of cmake output is this:
... -- Variable BAR in ./bar/ = -- Variable FOO in ./bar/ = Exists in both, parent AND in child scope. -- Variable BAR in ./ = Exists in parent scope only. -- Variable FOO in ./ = Exists in both, parent AND in child scope. ...
Since the BAR variable is placed in the parent area, I expect it to be available in the current child area (and in the following) - just like the FOO variable, which defines the parent area to start with. But, as can be seen from the above lines, the BAR variable is empty in ./bar/CMakeLists.txt , which leads me to the following questions:
Why is the changed parent region inaccessible to the child scope, ./bar/ ? Can this be mitigated? If so, how? And if not, then what to work around? Or am I completely missing something obvious?
Context: my project consists of several executable files and libraries. For a library, for example. BAR , I would like to set the variable bar_INCLUDE_DIR , which is added to the included paths of any dependent executable, i.e. target_include_directories( my_target PUBLIC bar_INCLUDE_DIR ) .