【Qt】【CMake】【CMakeLists.txt】-PROJECT_NAME 和 CMAKE_PROJECT_NAME 的区别
原帖:https://stackoverflow.com/questions/38938315/difference-between-cmake-project-name-and-project-name#:~:text=The%20difference%20is%20that%20CMAKE_PROJECT_NAME%20is%20the%20name,The%20difference%20is%20recognizable%20from%20the%20following%20test.
【CMake】【CMakeLists.txt】-PROJECT_NAME 和 CMAKE_PROJECT_NAME 的区别
两个项目名称宏的不同之处,在于:
1.CMAKE_PROJECT_NAME
是“根项目名称”,
是指最近一个命令指定的项目名称开始。
【译者goodmao注释】:
例如:下面的例子中,
project(B) #指定项目名字。
add_subdirectory(test2) #给根项目B,添加子项目test2。
那么,test2所在的“根项目”,就是B项目;
所以,根项目B的子项目test2中的 CMakeLists.txt 中:
CMAKE_PROJECT_NAME,指的是根项目 B;
PROJECT_NAME,指的是子项目test2本身。
2.PROJECT_NAME
是来自上一个项目调用,
无论包含该命令的文件的位置在哪儿。
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.
1.文件目录结构如下:
File structure:
|-CMakeLists.txt
\-test2
|-CMakeLists.txt
\-test3
\-CMakeLists.txt
2.根项目的CMake编译脚本:
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}")
3.子项目test2的CMake编译脚本:
test2/CMakeLists.txt:
project(D)
message("<< ${CMAKE_PROJECT_NAME} / ${PROJECT_NAME}")
add_subdirectory(test3)
project(E)
message("<< ${CMAKE_PROJECT_NAME} / ${PROJECT_NAME}")
4.孙子项目test3的CMake编译脚本:
test2/test3/CMakeLists.txt:
project(F)
message("<<< ${CMAKE_PROJECT_NAME} / ${PROJECT_NAME}")
5.相关的输出结果:
The relevant output is:
< A / A
< B / B
<< B / D
<<< B / F
<< B / E
< B / B
< C / C
【注意】:
在所有的子项目test2和孙子项目test3中,根项目名称(CMAKE_PROJECT_NAME
),都是B。
In the sub-directories, always B is the value for CMAKE_PROJECT_NAME
.
= = =