多个CMakeLists [英] Multiple CMakeLists
问题描述
假设我的目录结构如下:
Suppose I have a directory structure as follows:
/main.cpp
/CMakeLists.txt
/foo/foo.cpp
/foo/CMakeLists.txt
其中我的 /CMakeLists.txt
文件包含以下内容:
where my /CMakeLists.txt
file contains the following:
project(Test)
add_subdirectory("foo")
add_executable(Test main.cpp foo/foo.cpp)
target_link_libraries(Test ${OpenNI_LIB})
和我的 /foo/CMakeLists.txt
文件包含以下内容:
and my /foo/CMakeLists.txt
file contains the following:
find_library(OpenNI REQUIRED)
当我在第一个 CMakeLists.txt
中使用行 add_subdirectory( foo)
时,实际上会发生什么?是否在 foo
中搜索第二个 CMakeLists.txt
文件,并将内容添加到第一个文件中?定义第二个文件的任何变量在第一个文件中可用吗?特别是在此示例中,假设变量 $ {OpenNI_LIB}
在第二个变量中定义,则该变量会在第一个变量中被识别吗?
When I use the line add_subdirectory("foo")
in the first CMakeLists.txt
, what actually happens? Does it search for a second CMakeLists.txt
file in foo
, and add the contents to the first? Will any variables defined the second file be available in the first? And specifically in this example, will the variable ${OpenNI_LIB}
be recognised in the first, given that it is defined in the second?
谢谢。
推荐答案
它是否搜索第二个CMakeLists.txt文件在foo
Does it search for a second CMakeLists.txt file in foo
是的,确实如此
并将内容添加到第一个吗?
and add the contents to the first?
不是。它执行许多配置操作,例如查找库等,并生成单独的Makefile和/或其他构建时工件。
No it doesn't. It performs a number of configuring actions such as finding libraries etc and a generates separate Makefile and/or other build-time artifacts.
在此示例中,假设变量$ {OpenNI_LIB}是在第二个变量中定义的,那么它会在第一个变量中被识别吗?
And specifically in this example, will the variable ${OpenNI_LIB} be recognised in the first, given that it is defined in the second?
否,除非这样的构造
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 /
和自己的 CMakeL ists.txt
,然后在 bar /
的 CMakeLists.txt
$ {OpenNI_LIB}
By default variables defined in a subdirectory's CMakeLists.txt are also defined in the subdirectory's subdirectories, that is if foo/
in turn contained bar/
with its own CMakeLists.txt
, then within bar/
's CMakeLists.txt
${OpenNI_LIB} would be set.
PS 消息(状态某些带有$ {VAR}的消息)在 CMakeLists.txt
的可疑位置是您的朋友。只需查看cmake输出即可。
P.S. message(STATUS "Some message with ${VAR}") in doubtful places of CMakeLists.txt
is your friend. Just look into cmake output.
这篇关于多个CMakeLists的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!