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

前端 未结 4 1407
离开以前
离开以前 2020-12-22 22:29

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

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

wh

4条回答
  •  离开以前
    2020-12-22 23:10

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

    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:

    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_DESC as a string-constant:

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

    note: the --abbrev=N argument to git describe might be useful to achieve consistent version output independent of a users git configuration.

提交回复
热议问题