What is the difference between CMAKE_PROJECT_NAME and PROJECT_NAME?
From the documentation:
CMAKE_PROJECT_NAME
The name of the current project.
This specifies name of the current project from the closest inherited project() command.
PROJECT_NAME
Name of the project given to the project command.
This is the name given to the most recent project() command.
I don't understand the difference.
When should I use CMAKE_PROJECT_NAME
? When should I use PROJECT_NAME
?
Name of the project given to the project command. This is the name given to the most recently called project() command in the current directory scope or above. To obtain the name of the top level project, see the CMAKE_PROJECT_NAME variable.
The path to the top level of the source tree. This is the full path to the top level of the current CMake source tree. For an in-source build, this would be the same as CMAKE_BINARY_DIR .
CMakeLists. txt file contains a set of directives and instructions describing the project's source files and targets (executable, library, or both). When you create a new project, CLion generates CMakeLists. txt file automatically and places it in the project root directory.
Add a subdirectory to the build. Adds a subdirectory to the build. The source_dir specifies the directory in which the source CMakeLists.
From the documentation, I don't get the difference between the two variables.
The difference is that CMAKE_PROJECT_NAME
is the name from the last project
call from the root CMakeLists.txt, while PROJECT_NAME
is from the last project
call, regardless from the location of the file containing the command.
The difference is recognizable from the following test.
File structure:
|-CMakeLists.txt \-test2 |-CMakeLists.txt \-test3 \-CMakeLists.txt
CMakeLists.txt:
cmake_minimum_required(VERSION 3.0) project(A) message("< ${CMAKE_PROJECT_NAME} / ${PROJECT_NAME}") project(B) message("< ${CMAKE_PROJECT_NAME} / ${PROJECT_NAME}") add_subdirectory(test2) message("< ${CMAKE_PROJECT_NAME} / ${PROJECT_NAME}") project(C) message("< ${CMAKE_PROJECT_NAME} / ${PROJECT_NAME}")
test2/CMakeLists.txt:
project(D) message("<< ${CMAKE_PROJECT_NAME} / ${PROJECT_NAME}") add_subdirectory(test3) project(E) message("<< ${CMAKE_PROJECT_NAME} / ${PROJECT_NAME}")
test2/test3/CMakeLists.txt:
project(F) message("<<< ${CMAKE_PROJECT_NAME} / ${PROJECT_NAME}")
The relevant output is:
< A / A < B / B << B / D <<< B / F << B / E < B / B < C / C
In the sub-directories, always B is the value for CMAKE_PROJECT_NAME
.
It may help, if we look at it this way: CMAKE_PROJECT_NAME
is a global
and PROJECT_NAME
is a local
name ;)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With