CMake: Get root project name

Viewed 1621

Suppose I have following C project structure:

├── CMakeLists.txt
├── include
│   ├── header.h
├── README.md 
├── src
│   └── src.c
└── test
    ├── CMakeLists.txt
    └── test.c

The CMakeLists.txt in the root directory contains a project called cproj

PROJECT(cproj C)

Also in test/CMakeLists.txt, I have another project

PROJECT(test C)

The question is how can I assess the project name cproj in the CMakeLists.txt in test directory?

1 Answers

If I gather your use case correctly, you should probably do as follows:

  1. test is quite likely not a standalone sub-project. It should not have a project directive. If you remove it, it will now be under the cproj project.

  2. Now you looking for the project name you've set in the root, it should be available via the CMAKE_PROJECT_NAME variable.

Related