如何获取包含当前子项目的项目名称?

How can I get the name of the project containing the current sub-project?

使用 cmake,在 CMakeLists.txt 文件中,我可以使用 CMAKE_PROJECT_NAME 获取根项目的名称,使用 PROJECT_NAME 获取当前子项目的名称.

有没有办法获取当前上一级的项目名称?

您可以检查 PROJECT_NAME before 当前范围内的 project() 调用,这将为您提供 [=16= 的项目名称]父范围:

# Gives the project name of the parent scope.
message(${PROJECT_NAME})

# Define the project name for the current scope.
project(MyInnerProj)

# Now, gives the project name of the current scope: 'MyInnerProj'.
message(${PROJECT_NAME})