Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between CMAKE_PROJECT_NAME and PROJECT_NAME?

Tags:

cmake

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?

like image 312
over_optimistic Avatar asked Aug 14 '16 01:08

over_optimistic


People also ask

What is Project_name in CMake?

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.

What is Cmake_source_dir?

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 .

What is CMakeLists txt?

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.

What does Add_subdirectory do in CMake?

Add a subdirectory to the build. Adds a subdirectory to the build. The source_dir specifies the directory in which the source CMakeLists.


2 Answers

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.

like image 143
usr1234567 Avatar answered Sep 25 '22 15:09

usr1234567


It may help, if we look at it this way: CMAKE_PROJECT_NAME is a global and PROJECT_NAME is a local name ;)

like image 30
Yas Avatar answered Sep 23 '22 15:09

Yas