Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
735 views
in Technique[技术] by (71.8m points)

cmake - Difference between CMAKE_CURRENT_SOURCE_DIR and CMAKE_CURRENT_LIST_DIR

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...