MHebes
MHebes

Reputation: 3247

CMake multiple calls to project()

What are the implications of / possible reasons for calling project() from subdirectories' CMakeLists?

i.e.

├── CMakeLists.txt     <-- top-level project() call
├── proj1
|  ├── CMakeLists.txt  <-- project()
├── proj2
|  └── CMakeLists.txt  <-- project()
└── proj3
   └── CMakeLists.txt  <-- project()

I have seen this in codebases before, usually after a refactor.

Is this ever useful? Couldn't you just define targets with add_library/add_executable instead?

Is this actually harmful, or innocuous?

The docs only really talk about the special case of the top-level project() call, but don't get into reasons for calling it in subdirectories.

Upvotes: 1

Views: 1067

Answers (1)

fabian
fabian

Reputation: 82471

The benefit of doing this is that you could use one of your subprojects on its own or use it as part of some other project that does not require its siblings.

There should be no issue with doing this without the parent dir. Afaik there is no issue with this. In fact the documentation of PROJECT_NAME states that there could be multiple project commands in use:

[...]

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.

I work on a software package that contains > 10 project commands and there never was an issue with that.

The visual studio generator creates one solution file per project() though but a unnecessary files in the build directory shouldn't be an issue.

Upvotes: 4

Related Questions