cmake project version - last guy wins?

How can one scope the cmake project version? It appears the project version string is last-guy wins. Say I have top project CMakeLists.txt that sets version, then it includes some other component by via add_subdirectory, but this component will then override my project version.

There are multiple variables in CMake related to project versions:

  • There’s PROJECT_VERSION, which refers to the version specified in the last called project() command. I believe that’s what you’re referring to in your question.
  • There’s {ProjectName}_VERSION, where {ProjectName} is a placeholder for the name of a project. This variable refers to the version specified in the project(ProjectName) call.
  • There’s CMAKE_PROJECT_VERSION, which refers to the version of the top-level (i.e. first) project() call. This may be what you’re looking for.

All of these variables also have the variants _MAJOR, _MINOR, _PATCH, and _TWEAK for the individual version components. See the docs of the individual variables for more details.