A CMake module for automatically generating version information from Git for C++ and C projects.
- Automatic extraction of version information from Git
- Support for C++ headers (
version.hpp), C++20 modules (version.cppm), and C (version.h) - Easy integration via CPM (CMake Package Manager)
- Extracts: Version, Commit Hash, Branch, Commit Date
Add the following to your CMakeLists.txt:
include(cmake/CPM.cmake)
CPMAddPackage(
NAME cmake-git-versioning
GITHUB_REPOSITORY Katze719/cmake-git-versioning
GIT_TAG main # or a specific tag
)# After CPMAddPackage
include(cmake/cmake-git-versioning.cmake)
# Generate version.hpp (default)
generate_git_version()
# Add the generated directory to include paths
target_include_directories(your_target PRIVATE ${CMAKE_BINARY_DIR}/generated)
# Use in your code:
# #include "version.hpp"
# std::cout << version::VERSION << std::endl;# After CPMAddPackage
include(cmake/cmake-git-versioning.cmake)
# Generate version.cppm with CXX_MODULE option
generate_git_version(CXX_MODULE)
# Add the generated directory to module search paths
target_sources(your_target PRIVATE ${CMAKE_BINARY_DIR}/generated/version.cppm)
# Use in your code:
# import version;
# std::cout << version::VERSION << std::endl;# After CPMAddPackage
include(cmake/cmake-git-versioning.cmake)
# Generate version.h with C_VERSION option
generate_git_version(C_VERSION)
# Add the generated directory to include paths
target_include_directories(your_target PRIVATE ${CMAKE_BINARY_DIR}/generated)
# Use in your code:
# #include "version.h"
# printf("Version: %s\n", VERSION_STRING);# Custom template file and output directory
generate_git_version(
OUTPUT_DIR ${CMAKE_BINARY_DIR}/custom_version
TEMPLATE_FILE ${CMAKE_SOURCE_DIR}/custom_version.hpp.in
OUTPUT_FILE custom_version.hpp
)
# For C with custom options
generate_git_version(
C_VERSION
OUTPUT_DIR ${CMAKE_BINARY_DIR}/version_info
OUTPUT_FILE my_version.h
)The module also provides CMake variables that you can use directly in your CMakeLists.txt:
# After CPMAddPackage
include(cmake/cmake-git-versioning.cmake)
# Get git version info (sets CMake variables)
get_git_version_info()
# Use the variables in your CMake code
project(MyProject VERSION ${GIT_VERSION_MAJOR}.${GIT_VERSION_MINOR}.${GIT_VERSION_PATCH})
# Or use them in configure_file, message, etc.
message(STATUS "Building version: ${GIT_DESCRIBE}")
message(STATUS "Commit: ${GIT_COMMIT_HASH_SHORT}")
message(STATUS "Branch: ${GIT_BRANCH}")
# You can also use them in configure_file for custom templates
configure_file(
${CMAKE_SOURCE_DIR}/config.h.in
${CMAKE_BINARY_DIR}/config.h
@ONLY
)Available CMake Variables:
GIT_VERSION_MAJOR- Major version numberGIT_VERSION_MINOR- Minor version numberGIT_VERSION_PATCH- Patch version numberGIT_DESCRIBE- Full version string from git describeGIT_COMMIT_HASH_SHORT- Short commit hashGIT_COMMIT_HASH_FULL- Full commit hashGIT_BRANCH- Current branch nameGIT_COMMIT_DATE- Commit date
Note: generate_git_version() automatically calls get_git_version_info(), so the variables are available after calling either function.
version::MAJOR,version::MINOR,version::PATCH- Version numbers asinline constexpr intversion::VERSION- Full version string (e.g., "v1.2.3" or "v1.2.3-5-gabc1234")version::GIT_COMMIT_HASH_SHORT- Short commit hashversion::GIT_COMMIT_HASH_FULL- Full commit hashversion::GIT_BRANCH- Current branch nameversion::GIT_COMMIT_DATE- Commit dateversion::getVersionString()- Helper function for version stringversion::getFullVersionInfo()- Helper function for full version info
version::MAJOR,version::MINOR,version::PATCH- Version numbers asinline constexpr intversion::VERSION- Full version string (e.g., "v1.2.3" or "v1.2.3-5-gabc1234")version::GIT_COMMIT_HASH_SHORT- Short commit hashversion::GIT_COMMIT_HASH_FULL- Full commit hashversion::GIT_BRANCH- Current branch nameversion::GIT_COMMIT_DATE- Commit dateversion::getVersionString()- Exported helper function for version stringversion::getFullVersionInfo()- Exported helper function for full version info
VERSION_MAJOR,VERSION_MINOR,VERSION_PATCH- Version numbers as#defineVERSION_STRING- Full version stringGIT_COMMIT_HASH_SHORT- Short commit hashGIT_COMMIT_HASH_FULL- Full commit hashGIT_BRANCH- Current branch nameGIT_COMMIT_DATE- Commit date
#include "version.hpp"
#include <iostream>
int main() {
std::cout << "Version: " << version::VERSION << std::endl;
std::cout << "Full Info: " << version::getFullVersionInfo() << std::endl;
std::cout << "Commit: " << version::GIT_COMMIT_HASH_SHORT << std::endl;
return 0;
}import version;
import <iostream>;
int main() {
std::cout << "Version: " << version::VERSION << std::endl;
std::cout << "Full Info: " << version::getFullVersionInfo() << std::endl;
std::cout << "Commit: " << version::GIT_COMMIT_HASH_SHORT << std::endl;
return 0;
}#include "version.h"
#include <stdio.h>
int main() {
printf("Version: %s\n", VERSION_STRING);
printf("Major: %d, Minor: %d, Patch: %d\n",
VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH);
printf("Commit: %s\n", GIT_COMMIT_HASH_SHORT);
return 0;
}- CMake 3.10 or higher (3.28+ recommended for full C++20 module support)
- Git (for version generation)
- CPM
Optional requirements:
- C++20 compatible compiler (only required if using
CXX_MODULEoption)
MIT License - see LICENSE file