-
Notifications
You must be signed in to change notification settings - Fork 19
Adding a Version Number from a Configured Header File
Felipe Torrezan edited this page Aug 17, 2026
·
8 revisions
This interactive example describes how to set a CMake target's VERSION property, automatically configure a header file that contains both integer-formatted and string-formatted preprocessor symbols based on that version, and then consume those symbols from the application code.
VERSIONset_target_properties()get_property()string(REGEX MATCH ...)configure_file()target_include_directories()
A CMake project example is provided at examples/version:
| Project files |
|---|
CMakeLists.txt |
main.c |
version.h.in |
The main() function will print the application version information provided by version.h (configured by CMake).
- Perform the following tasks on
CMakeLists.txt(click to show/hide answers):
TODO 1: Set the target's VERSION to 1.2.3
set_target_properties(target PROPERTIES
VERSION 1.2.3
)TODO 2: Expose the target's VERSION to the application
get_target_property(App_VERSION target VERSION)
string(REGEX MATCH "^([0-9]+)\\.([0-9]+)\\.([0-9]+)" _dummy ${App_VERSION})
set(App_VERSION_MAJOR ${CMAKE_MATCH_1})
set(App_VERSION_MINOR ${CMAKE_MATCH_2})
set(App_VERSION_PATCH ${CMAKE_MATCH_3})TODO 3: Configure version.h from version.h.in
configure_file(
${CMAKE_CURRENT_SOURCE_DIR}/version.h.in
${CMAKE_CURRENT_BINARY_DIR}/include/version.h
)TODO 4: Add `build/include` to the target's include directories
target_include_directories(target PUBLIC "${CMAKE_CURRENT_BINARY_DIR}/include")- Perform the following task on
version.h.in(click to show/hide answers):
TODO 5: Import CMake's version variables as preprocessor-defined symbols
#define App_VERSION "@App_VERSION@"
#define App_VERSION_MAJOR @App_VERSION_MAJOR@
#define App_VERSION_MINOR @App_VERSION_MINOR@
#define App_VERSION_PATCH @App_VERSION_PATCH@- Finally build and test the project. Refer to the tutorial for more information.
Test project /home/.../cmake-tutorial/examples/version/build
Start 1: string-version-test
1/2 Test #1: string-version-test .............. Passed 0.21 sec
Start 2: integer-version-test
2/2 Test #2: integer-version-test ............. Passed 0.21 sec
100% tests passed out of 2
Note
Although this example focuses on an executable target, the same concepts apply to library targets.
This is the cmake-tutorial wiki. Back to Wiki Home
- IAR Compiler options in a CMake project
- IAR ILINK options in a CMake project
- Language-specific target options
- Selecting build types
- Using Ninja Multi-Config
- Filing a build log
- Multi-file compilation
- Invoking IAR binary utilities
- Use the IAR ELF Tool to convert executable targets to their binary formats
- Using IAR Build Tools with CMake Presets