考虑以下最小示例:
.
├── 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}" )
输出的相关部分cmake
这是:
...
-- 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.
...
由于变量BAR
被放置到父作用域中,我希望它在当前子作用域中也可用(以及在随后的子作用域中)——就像变量一样FOO
,它首先定义为父作用域。但从上面几行可以看出
多变的BAR
是空的./bar/CMakeLists.txt
,这让我
以下问题:
为什么是modified父作用域无法在子作用域中立即访问
范围,./bar/
?这种情况可以缓解吗?如果是,怎么办?如果不是,什么是
解决方法?或者我完全错过了一些明显的东西?
上下文:我的项目由多个可执行文件和库组成。为一个
图书馆,例如bar
,我想设置一个变量bar_INCLUDE_DIR
哪个
添加到任何相关可执行文件的包含路径中,即target_include_directories( my_target PUBLIC bar_INCLUDE_DIR )
.