赞
踩
本方法记录如何在程序中打印版本信息,比如 git commit id, git branch, git tag, 编译时间,编译用户等
目的:在编译时,记录 git commit 信息,打印到日志中,方便调试定位问题
举例:
目录结构如下
├── build.sh
├── CMakeLists.txt
├── config.h.in
├── makefile
└── src
├── CMakeLists.txt
└── main.c
先定义 config.h.in 文件,里面定义 SDK_PRJ_USER,SDK_PRJ_TIME,SDK_PRJ_COMMIT 等变量。
#ifndef _SDK_CONFIG_H_
#define _SDK_CONFIG_H_
#define SDK_BUILD_USER "@SDK_BUILD_USER@"
#define SDK_BUILD_TIME "@SDK_BUILD_TIME@"
#define SDK_BUILD_COMMIT "@SDK_BUILD_COMMIT@"
#endif /* _SDK_CONFIG_H_ */
在 main.c 里包含 config.h 文件,然后打印这些变量。
#include <stdio.h>
#include "config.h"
int main(int argc, char *argv[]) {
printf("time %s, user %s, commit %s\n", SDK_BUILD_TIME, SDK_BUILD_USER, SDK_BUILD_COMMIT);
return 0;
}
build.sh 文件内容如下:
# project info
SDK_PRJ_USER=`git config user.email`
SDK_PRJ_TIME=`date +%Y%m%d_%H%M%S`
SDK_PRJ_COMMIT=`git rev-parse --short HEAD`
cp config.h.in config.h
sed -i 's/#define SDK_BUILD_TIME.*/#define SDK_BUILD_TIME "'${SDK_PRJ_TIME}'"/g' config.h
sed -i 's/#define SDK_BUILD_USER.*/#define SDK_BUILD_USER "'${SDK_PRJ_USER}'"/g' config.h
sed -i 's/#define SDK_BUILD_COMMIT.*/#define SDK_BUILD_COMMIT "'${SDK_PRJ_COMMIT}'"/g' config.h
gcc src/main.c -I./
makefile 文件内容如下:
SDK_PRJ_USER=$(shell git config user.email) SDK_PRJ_TIME=$(shell date +%Y%m%d_%H%M%S) SDK_PRJ_COMMIT=$(shell git rev-parse --short HEAD) all: config demo config: cp config.h.in config.h sed -i 's/#define SDK_BUILD_TIME.*/#define SDK_BUILD_TIME "'$(SDK_PRJ_TIME)'"/g' config.h sed -i 's/#define SDK_BUILD_USER.*/#define SDK_BUILD_USER "'$(SDK_PRJ_USER)'"/g' config.h sed -i 's/#define SDK_BUILD_COMMIT.*/#define SDK_BUILD_COMMIT "'$(SDK_PRJ_COMMIT)'"/g' config.h demo: $(OBJ1) gcc src/main.c -I./ clean: rm -f a.out config.h
cmake_minimum_required(VERSION 3.5) PROJECT (DEMO) SET(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin) macro(git_cmd_result _git_result) # 宏的开始 find_package(Git QUIET) # 查找Git,QUIET静默方式不报错 if(GIT_FOUND) execute_process( # 执行一个子进程 COMMAND ${GIT_EXECUTABLE} ${git_cmd} # 命令,需要预定义 git_cmd 变量,制定 git 命令 OUTPUT_VARIABLE ${_git_result} # 输出字符串存入变量 OUTPUT_STRIP_TRAILING_WHITESPACE # 删除字符串尾的换行符 ERROR_QUIET # 对执行错误静默 WORKING_DIRECTORY # 执行路径 ${CMAKE_CURRENT_SOURCE_DIR} ) endif() endmacro() # 宏的结束 set(git_cmd rev-parse --short HEAD) # 查找 commit id git_cmd_result(SDK_BUILD_COMMIT) set(git_cmd config user.email) # 查找 git user email git_cmd_result(SDK_BUILD_USER) set(SDK_BUILD_TIME "") string(TIMESTAMP SDK_BUILD_TIME %Y%m%d_%H%M%S) # 配置文件 configure_file(${PROJECT_SOURCE_DIR}/config.h.in ${PROJECT_BINARY_DIR}/include/config.h) ADD_SUBDIRECTORY(src)
MESSAGE(STATUS "This is BINARY dir " ${PROJECT_BINARY_DIR})
INCLUDE_DIRECTORIES(${PROJECT_BINARY_DIR}/include)
ADD_EXECUTABLE(demo main.c)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。