方式1:通过add_definitions方式定义变量,在源程序中使用
A. CMakeLists.txt
cmake_minimum_required(VERSION 3.15)
project(test)
set(SRCS main.cpp)
# 执行git命令,并把结果重定向到自定义变量(此处是我的变量<GIT_VERSION>)。
execute_process(COMMAND git rev-parse HEAD OUTPUT_VARIABLE GIT_VERSION)
# 对得到的结果进行处理,尤其注意 \n,\t,\r之类的特殊字符,在cmake时没问题,但是生成的.cmake文件有问题,导致make出错。
string (REGEX REPLACE "[\n\t\r]" "" GIT_VERSION ${GIT_VERSION})
# 增加编译选项,把宏导入源码
add_definitions( -DGIT_VERSION=\"${GIT_VERSION}\")
add_executable(app ${SRCS})
B.C++ 源程序
#include <iostream>
using namespace std;
int main(){
cout << "[GitVersion] " << GIT_VERSION << endl;
cout << "[BuildDate] " << __DATE__ <<" "<< __TIME__ << endl;
return 0;
}
C.编译运行
cmake CMakeLists.txt
make
./app
D.结果输出
[root@tmp ]# ./app
[GitVersion] a53cd05f876ea870c4af2ab0fdf839014e741a7f
[BuildDate] Sep 13 2021 19:45:51
[root@tmp ]#
方式2:通过config.h.in
A. CMakeLists.txt
cmake_minimum_required (VERSION 3.15)
project (createProVersion)
# Add the variables we need
set (VERSION_MAJOR 2)
set (VERSION_MINOR 30)
set (SOFT_VERSION \"V${VERSION_MAJOR}.${VERSION_MINOR}\")
execute_process(COMMAND git rev-parse --short HEAD OUTPUT_VARIABLE GIT_COMMAND_ID)
execute_process(COMMAND date +"%Y%m%d%H%M" OUTPUT_VARIABLE COMPILE_TIME)
string (REGEX REPLACE "[\n\t\r]" "" GIT_COMMAND_ID \"${GIT_COMMAND_ID}\")
string (REGEX REPLACE "[\n\t\r]" "" COMPILE_TIME ${COMPILE_TIME})
# Specify the configuration file from which the header file will be generated
configure_file (config.h.in config.h @ONLY)
add_executable (${PROJECT_NAME} main.cpp)
# add a directory with header files to see the generated file
include_directories ($ {CMAKE_CURRENT_BINARY_DIR})
message("GIT_CID:${GIT_COMMAND_ID}")
message("COMPILE_TIME:${COMPILE_TIME}")
B:config.h.ini 文件(config.h文件会自动生成)
#ifndef CONFIG_H_IN
#define CONFIG_H_IN
/**
* define 会直接根据规则来替换
*/
#define VERSION_MAJOR @VERSION_MAJOR@
#define VERSION_MINOR @VERSION_MINOR@
#define SOFT_VERSION @SOFT_VERSION@
#define GIT_COMMAND_ID @GIT_COMMAND_ID@
#define COMPILE_TIME @COMPILE_TIME@
#endif
C:C++ 源程序
#include <iostream>
#include "config.h" // Add an automatically generated configuration file
int main ()
{
// Use variables from CMakeLists.txt
// std :: cout << "project name:" << PROJECT_NAME << std :: endl;
std :: cout << "version:" << SOFT_VERSION << std :: endl;
std :: cout << "version major:" << VERSION_MAJOR << std :: endl;
std :: cout << "version minor:" << VERSION_MINOR << std :: endl;
std :: cout << "git command id:" << GIT_COMMAND_ID <<std :: endl;
std :: cout << "compile time:" << COMPILE_TIME << std :: endl;
return 0;
}
D:编译运行
cmake CMakeLists.txt
make
./createProVersion
E: 输出
[root@tmp ]# ./createProVersion
version:V2.30
version major:2
version minor:30
git command id:a53cd05
compile time:202109131601
[root@tmp ]#
说明:在执行cmake CMakeLists.txt ,生成config.h,该文件内容如下
#ifndef CONFIG_H_IN
#define CONFIG_H_IN
// #define PROJECT_NAME createProVersion
#define VERSION_MAJOR 2
#define VERSION_MINOR 30
#define SOFT_VERSION "V2.30"
#define GIT_COMMAND_ID "a53cd05"
#define COMPILE_TIME "202109131607"
#endif