When managing CMake-based projects, it's often helpful to include version information for the project. Defining a version not only provides clarity for releases, but also enables you to access version components (major, minor, patch) directly within CMake scripts. This can be useful for packaging, conditional logic, or displaying version info at build time. This tutorial demonstrates how to set CMake project version.
Below is a simple example of how to define a version in the CMakeLists.txt file:
cmake_minimum_required(VERSION 3.27)
project(myapp VERSION 1.2.3)
set(CMAKE_CXX_STANDARD 17)
message(STATUS ${CMAKE_PROJECT_VERSION})
message(STATUS ${CMAKE_PROJECT_VERSION_MAJOR})
message(STATUS ${CMAKE_PROJECT_VERSION_MINOR})
message(STATUS ${CMAKE_PROJECT_VERSION_PATCH})
add_executable(${PROJECT_NAME} main.cpp)
The version can be specified in the project command by using VERSION argument. In this example, the project version number is set to 1.2.3. CMake automatically breaks this down into three separate variables that you can reference in the configuration or build process.
To generate the build files, simply run:
cmake -S . -B build
You'll see the version information printed in the configuration output:
1.2.3
1
2
3
In summary, setting a project version in CMake makes the build process clearer and easier to maintain. It also supports consistent versioning, simplifies release management, and helps track software changes over time.
Leave a Comment
Cancel reply