您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關(guān)如何在C++二進制文件中注入git信息的內(nèi)容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
創(chuàng)建頭文件version.hh,內(nèi)容如下,其中聲明了表示git信息的數(shù)據(jù)結(jié)構(gòu)
#pragma once #include <string> namespace my_app { struct Version { static const std::string GIT_SHA1; static const std::string GIT_BRANCH; static const std::string GIT_DATE; static const std::string GIT_COMMIT_SUBJECT; }; }
創(chuàng)建模板文件,version.cc.in,其中定義了Version中靜態(tài)變量的值。其中@XX@表示該變量通過cmake注入
#include "version.hh"
#include "version.hh" using namespace my_app; const std::string Version::GIT_SHA1 = "@GIT_SHA1@"; const std::string Version::GIT_BRANCH = "@GIT_BRANCH@"; const std::string Version::GIT_DATE = "@GIT_DATE@"; const std::string Version::GIT_COMMIT_SUBJECT = "@GIT_COMMIT_SUBJECT@";
創(chuàng)建main.cc
#include <iostream> #include "version.hh" using namespace my_app; int main() { std::cout << "building sha1: " << Version::GIT_SHA1 << std::endl; std::cout << "building branch: " << Version::GIT_BRANCH << std::endl; std::cout << "building date: " << Version::GIT_DATE << std::endl; std::cout << "building commit subject: " << Version::GIT_COMMIT_SUBJECT << std::endl; return 0;
創(chuàng)建CMakeLists.txt
cmake_minimum_required(VERSION 3.5) project (inject_git) set(GIT_EXECUTABLE /usr/bin/git) # 執(zhí)行g(shù)it rev-parse HEAD, 生成commitid, 將結(jié)果保存于變量GIT_SHA1 execute_process(COMMAND "${GIT_EXECUTABLE}" rev-parse HEAD WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}" OUTPUT_VARIABLE GIT_SHA1 ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE) # 執(zhí)行g(shù)it rev-parse --abbrev-ref HEAD, 生成branch name, 將結(jié)果保存于變量GIT_BRANCH execute_process(COMMAND "${GIT_EXECUTABLE}" rev-parse --abbrev-ref HEAD WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}" OUTPUT_VARIABLE GIT_BRANCH ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE) # 執(zhí)行g(shù)it log -1 --format=%ad --date=local, 生成提交日期, 將結(jié)果保存于變量GIT_DATE execute_process(COMMAND "${GIT_EXECUTABLE}" log -1 --format=%ad --date=local WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}" OUTPUT_VARIABLE GIT_DATE ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE) # 執(zhí)行g(shù)it log -1 --format=%s, 生成提交日志, 將結(jié)果保存于變量GIT_COMMIT_SUBJECT execute_process(COMMAND "${GIT_EXECUTABLE}" log -1 --format=%s WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}" OUTPUT_VARIABLE GIT_COMMIT_SUBJECT ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE) # 上述變量初始化后,將其注入到模板文件version.cc.in中,生成version.cc configure_file("${CMAKE_CURRENT_SOURCE_DIR}/version.cc.in" "${CMAKE_CURRENT_BINARY_DIR}/version.cc" @ONLY) set(SOURCES main.cc version.cc ) # 由version.cc和main.cc生成二進制 add_executable(inject_git ${SOURCES})
二進制文件執(zhí)行結(jié)果如下:
$ ./inject_git building sha1: 74b25fc30d18001f72ec0cf0495165441df40a09 building branch: master building date: Fri May 28 14:49:16 2021 building commit subject: add again
感謝各位的閱讀!關(guān)于“如何在C++二進制文件中注入git信息”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。