如何将 git commit-number 包含到 C++ 可执行文件中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6526451/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 05:37:58  来源:igfitidea点击:

how to include git commit-number into a c++ executable?

c++gitmakefile

提问by kirill_igum

i use git as a version tracker for my c++ project.

我使用 git 作为我的 c++ 项目的版本跟踪器。

sometimes i need to repeat a calculation and i would like to know which version of the program i used.

有时我需要重复计算,我想知道我使用的是哪个版本的程序。

what would be a good way to put a # of the commit into the main executable? in other words. i'd like the program to tell me the # of the current commit in an introductory message as i run the program.

将提交的 # 放入主可执行文件的好方法是什么?换句话说。当我运行程序时,我希望程序在介绍性消息中告诉我当前提交的#。

one way i can think of is to make the c++ program lunch "git log" from shell and extract the commit # but i'm not sure how to do it during make.

我能想到的一种方法是从 shell 制作 C++ 程序午餐“git log”并提取提交 # 但我不知道如何在制作过程中做到这一点。

(I use linux)

(我用的是linux)

采纳答案by bdonlan

Probably the easiest way to do this would be to add to your makefile a rule to generate a .c file with the current git commit ID:

可能最简单的方法是在你的 makefile 中添加一个规则来生成一个带有当前 git commit ID 的 .c 文件:

gitversion.c: .git/HEAD .git/index
    echo "const char *gitversion = \"$(shell git rev-parse HEAD)\";" > $@

Now simply add gitversion.cto your build process as normal. Make sure to remove it on make clean, and add it to .gitignoreso it's not added to the git repository accidentally. Add an extern const char *gitversion;to a header somewhere, and you can access it like that.

现在只需gitversion.c像往常一样添加到您的构建过程中即可。确保在 上将其删除make clean,然后将其添加到 中,.gitignore以免意外添加到 git 存储库中。extern const char *gitversion;在某处添加一个标题,您可以像这样访问它。

回答by Naszta

I do the following in CMakeLists.txt:

我在 CMakeLists.txt 中执行以下操作:

IF(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/.git)
  FIND_PACKAGE(Git)
  IF(GIT_FOUND)
    EXECUTE_PROCESS(
      COMMAND ${GIT_EXECUTABLE} rev-parse --short HEAD
      WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
      OUTPUT_VARIABLE "kml2maps_BUILD_VERSION"
      ERROR_QUIET
      OUTPUT_STRIP_TRAILING_WHITESPACE)
    MESSAGE( STATUS "Git version: ${kml2maps_BUILD_VERSION}" )
  ELSE(GIT_FOUND)
    SET(kml2maps_BUILD_VERSION 0)
  ENDIF(GIT_FOUND)
ENDIF(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/.git)

CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/kml2mapsVersion.h.in ${CMAKE_CURRENT_BINARY_DIR}/kml2mapsVersion.h @ONLY)

So the git rev-parse --short HEAD's output is good to build in the binary.

所以git rev-parse --short HEAD的输出很好地构建在二进制文件中。

回答by gauteh

I use git describeto get a version which either uses a tag or commit number. This usually gives nice versions like: v0.1-1-g787c667if the tip of the branch has additional commits above the 'v0.1' tag.

git describe用来获取一个使用标签或提交号的版本。这通常会提供不错的版本,例如:v0.1-1-g787c667如果分支的尖端在 'v0.1' 标签上方有额外的提交。

The git command I use is: git describe --tags --always. I usually use it with the SCons build system and define it as a constant, relevant parts of the SConstruct:

我使用的 git 命令是:git describe --tags --always. 我通常将它与 SCons 构建系统一起使用,并将其定义为 SConstruct 的常量、相关部分:

import os, sys 
from subprocess import *

def getGitDesc():   
  return Popen('git describe --tags --always', stdout=PIPE, shell=True).stdout.read ().strip ()

GIT_DESC = getGitDesc () 
print "Building " + getGitDesc () + ".." 
env = Environment ()

# set up environment 
env.Append (CPPDEFINES = { 'GIT_DESC' : ('\"%s\"' % GIT_DESC) } )

# build your program
env.Program (....)

In the C or C++ program I can now access GIT_DESCas a string-constant:

在 C 或 C++ 程序中,我现在可以GIT_DESC作为字符串常量访问:

# include <iostream>

using namespace std;

int main (int argc, char ** argv) {
  cout << "Version: " << GIT_DESC << endl;
  return 42;
}

note: the --abbrev=Nargument to git describemight be useful to achieve consistent version output independent of a users git configuration.

注意:--abbrev=N参数 togit describe可能有助于实现独立于用户 git 配置的一致版本输出。

回答by Hyman Deeth

If you're using Qt, put this in your project's .pro file:

如果您使用 Qt,请将其放入项目的 .pro 文件中:

win32:DEFINES += GIT_BIN='C:\Git\bin\git'
# or 'C:\Progra~1\Git\bin\git' - ymmv with putting spaces in here
win32:DEFINES += GIT_REVISION='\"$$system($${GIT_BIN} rev-parse --short HEAD)\"'
unix:DEFINES += GIT_REVISION='\"$$system(git rev-parse --short HEAD)\"'

Then use GIT_REVISIONin your code as in the other answers - it behaves as const char *.

然后GIT_REVISION在您的代码中像在其他答案中一样使用 - 它的行为与const char *.

(Thanks to Alexander Barthel, who I plundered this tip from.)

(感谢 Alexander Barthel,我从他那里掠夺了这个小费。)