CMAKE_CURRENT_SOURCE_DIR 和 CMAKE_CURRENT_LIST_DIR 的区别 [英] Difference between CMAKE_CURRENT_SOURCE_DIR and CMAKE_CURRENT_LIST_DIR

查看:44
本文介绍了CMAKE_CURRENT_SOURCE_DIR 和 CMAKE_CURRENT_LIST_DIR 的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来自维基:

CMAKE_CURRENT_SOURCE_DIR这是当前处理的CMakeLists.txt所在目录

CMAKE_CURRENT_LIST_DIR(自 2.8.3 起)这是当前正在处理的列表文件的目录.

来自文档:

CMAKE_CURRENT_SOURCE_DIR: 当前正在处理的源目录的路径.这是 cmake 当前正在处理的源目录的完整路径.

CMAKE_CURRENT_LIST_DIR: 当前正在处理的列表文件的完整目录.当 CMake 处理项目中的列表文件时,此变量将始终设置为当前正在处理的列表文件 (CMAKE_CURRENT_LIST_FILE) 所在的目录.该值具有动态范围.当 CMake 开始处理源文件中的命令时,它会将此变量设置为该文件所在的目录.当 CMake 完成处理文件中的命令时,它会恢复以前的值.因此,宏或函数内变量的值是调用调用堆栈最底部条目的文件目录,而不是包含宏或函数定义的文件目录.

在什么情况下这些变量会有不同的值?

解决方案

变量 CMAKE_CURRENT_SOURCE_DIRCMAKE_CURRENT_LIST_DIR 可能引用另一个文件包含的 CMake 列表文件的不同目录,include 命令.例如,如果 CMakeLists.txt 存在于目录 project 中并包含以下指令

include(src/CMakeLists.txt)

然后在处理 src/CMakeLists.txtCMAKE_CURRENT_LIST_DIR 将引用 project/srcCMAKE_CURRENT_SOURCE_DIR仍然指向外部目录project.

CMAKE_CURRENT_LIST_DIR 当您需要定位当前正在处理的 CMakeLists.txt 文件旁边的资源文件(如模板文件或批处理脚本)时会派上用场.>


注意:当使用 add_subdirectory() 命令而不是 include() 时,行为是不同的,当 src/CMakeLists.txt 正在处理中,两个变量都将指向 project/src.

From the Wiki:

CMAKE_CURRENT_SOURCE_DIR this is the directory where the currently processed CMakeLists.txt is located in

CMAKE_CURRENT_LIST_DIR (since 2.8.3) this is the directory of the listfile currently being processed.

From the Docs:

CMAKE_CURRENT_SOURCE_DIR: The path to the source directory currently being processed. This the full path to the source directory that is currently being processed by cmake.

CMAKE_CURRENT_LIST_DIR: Full directory of the listfile currently being processed. As CMake processes the listfiles in your project this variable will always be set to the directory where the listfile which is currently being processed (CMAKE_CURRENT_LIST_FILE) is located. The value has dynamic scope. When CMake starts processing commands in a source file it sets this variable to the directory where this file is located. When CMake finishes processing commands from the file it restores the previous value. Therefore the value of the variable inside a macro or function is the directory of the file invoking the bottom-most entry on the call stack, not the directory of the file containing the macro or function definition.

Under what circumstances would these variables hold different values?

解决方案

The variables CMAKE_CURRENT_SOURCE_DIR and CMAKE_CURRENT_LIST_DIR may refer to different directories for a CMake list file that is included by another file with the include command. E.g., if a CMakeLists.txt is present in a directory project and contains the following directive

include(src/CMakeLists.txt)

then while src/CMakeLists.txt is being processed CMAKE_CURRENT_LIST_DIR will refer to project/src whereas CMAKE_CURRENT_SOURCE_DIR still points to the outer directory project.

CMAKE_CURRENT_LIST_DIR comes in handy when you need to locate resource files like template files or batch scripts that are located next to the CMakeLists.txt file currently being processed.


Note: When using the add_subdirectory() command rather than include(), the behavior is different, and when src/CMakeLists.txt is being processed, both variables will point to project/src.

这篇关于CMAKE_CURRENT_SOURCE_DIR 和 CMAKE_CURRENT_LIST_DIR 的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆