【问题标题】:Multiple CMakeLists多个 CMakeLists
【发布时间】:2015-08-22 07:36:36
【问题描述】:

假设我的目录结构如下:

/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})

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

find_library(OpenNI REQUIRED)

当我在第一个CMakeLists.txt 中使用add_subdirectory("foo") 行时,实际发生了什么?它是否在foo 中搜索第二个CMakeLists.txt 文件,并将内容添加到第一个文件中?定义第二个文件的任何变量在第一个文件中是否可用?特别是在这个例子中,变量${OpenNI_LIB} 是否会在第一个中被识别,因为它是在第二个中定义的?

谢谢。

【问题讨论】:

    标签: cmake


    【解决方案1】:

    它是否在 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} 将被设置。

    附: message(STATUS "Some message with ${VAR}")CMakeLists.txt 的可疑地方是你的朋友。只需查看 cmake 输出即可。

    【讨论】:

      猜你喜欢
      • 2020-05-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-31
      • 1970-01-01
      相关资源
      最近更新 更多