多重CMakeLists

多重CMakeLists,cmake,Cmake,假设我有一个目录结构,如下所示: /main.cpp /CMakeLists.txt /foo/foo.cpp /foo/CMakeLists.txt 其中我的/CMakeLists.txt文件包含以下内容: project(Test) add_subdirectory("foo") add_executable(Test main.cpp foo/foo.cpp) target_link_libraries(Test ${OpenNI_LIB}) find_library(OpenNI R

假设我有一个目录结构,如下所示:

/main.cpp
/CMakeLists.txt
/foo/foo.cpp
/foo/CMakeLists.txt
其中我的
/CMakeLists.txt
文件包含以下内容:

project(Test)
add_subdirectory("foo")
add_executable(Test main.cpp foo/foo.cpp)
target_link_libraries(Test ${OpenNI_LIB})
find_library(OpenNI REQUIRED)
我的
/foo/CMakeLists.txt文件包含以下内容:

project(Test)
add_subdirectory("foo")
add_executable(Test main.cpp foo/foo.cpp)
target_link_libraries(Test ${OpenNI_LIB})
find_library(OpenNI REQUIRED)
当我在第一个
CMakeLists.txt
中使用行
add_子目录(“foo”)
时,实际发生了什么?它是否在
foo
中搜索第二个
CMakeLists.txt
文件,并将内容添加到第一个文件中?第二个文件中定义的变量在第一个文件中可用吗?特别是在这个例子中,如果变量
${OpenNI_LIB}
是在第二个例子中定义的,那么它会在第一个例子中被识别吗

谢谢

它是否在foo中搜索第二个CMakeLists.txt文件

是的

并将内容添加到第一个

不,没有。它执行许多配置操作,如查找库等,并生成单独的Makefile和/或其他构建时工件

特别是在这个例子中,如果变量${OpenNI_LIB}是在第二个例子中定义的,那么它会在第一个例子中被识别吗

不,除非有这样的结构

find_library(OpenNI REQUIRED) # this sets variables for OpenNI
                              # in the context of foo/CMakeLists.txt
set(OpenNI_LIB ${OpenNI_LIB} PARENT_SCOPE) # this copies ${OpenNI_LIB}
                              # into the context of /CMakeLists.txt
foo/CMakeLists.txt中使用

默认情况下,子目录的CMakeLists.txt中定义的变量也在子目录的子目录中定义,也就是说,如果
foo/
依次包含
bar/
和它自己的
CMakeLists.txt
,然后在
bar/
CMakeLists.txt
${OpenNI_LIB}中设置

CMakeLists.txt
的可疑位置,p.S.是你的朋友。只需查看cmake输出