在c ++二进制文件中嵌入git描述字符串 [英] embed git describe string in c++ binary

查看:100
本文介绍了在c ++二进制文件中嵌入git描述字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将git describe命令的结果嵌入到我的c ++二进制文件中,以便我可以在运行时访问它。



我知道我可以做些什么像这样

  g ++ -DVERSION_NUMBER = 2345 

然后在我的代码中,我可以这样做:

  std :: cout<<<< ; VERSION_NUMBER<<的std :: ENDL; 

如果版本号的值是一个字符串,例如输出的git describe命令。



所以有办法做这样的事情吗?



谢谢。

解决方案

如果您使用automake,那么您可以使用类似下面的内容。我已经使用它很长一段时间了,它可以很好地实现你所要求的。



Makefile.am

code $:b
$ b $ $ p $ BUILT_SOURCES = git_info.h
EXTRA_DIST = echo_git_info.sh win_config.h



(主版本配方)



git_info.h:$(HEADERS)$(SOURCES)
echo_git_info.sh> git_info.h

echo_git_info.sh 中:

 #! / bin / sh 

函数echo_git_info {
if [[$(which git 2> / dev / null)]]
then
local STATUS
STATUS = $(git status 2> / dev / null)
if [[-z $ STATUS]]
然后
返回
fi
echo`git symbolic -ref HEAD 2> / dev / null | cut -b 12 -` -`git log --pretty = format:\%h \-1`,
return
fi
}

echo//自动生成的源/构建信息。
echo#define GIT_SOURCE_DESC \`echo_git_info````

以上将导致文件 git_info.h 中定义变量 GIT_SOURCE_DESC 。因此,在你的 main.cpp 中,你可以:

  #include <&的iostream GT; 
#includeginkgo_info.h

int main(int argc,char * argv []){
std :: cout<< 来源是:<< $ GIT_SOURCE_DESC<<的std :: ENDL;
}


How can I have the result of the git describe command embedded into my c++ binary so that I can access it at run time.

I know I can do something like this

g++ -DVERSION_NUMBER=2345

And then in my code I can do:

std::cout << VERSION_NUMBER << std::endl;

This is great but won't work if the value of version number is a string such as the output of the git describe command.

So is there way to do such a thing?

Thanks.

解决方案

If you use automake, then you can use something like the following. I have been using it for a long time now, and it works fine to achieve what you are asking for.

In Makefile.am:

BUILT_SOURCES = git_info.h
EXTRA_DIST = echo_git_info.sh win_config.h

.
.
(main build recipes)
.
.

git_info.h: $(HEADERS) $(SOURCES)
    echo_git_info.sh > git_info.h

In echo_git_info.sh:

#! /bin/sh

function echo_git_info {
    if [[ $(which git 2> /dev/null) ]]
    then
        local STATUS
        STATUS=$(git status 2>/dev/null)
        if [[ -z $STATUS ]]
        then
            return
        fi
        echo "`git symbolic-ref HEAD 2> /dev/null | cut -b 12-`-`git log --pretty=format:\"%h\" -1`, "
        return
    fi
}

echo "// Auto-generated source/build information."
echo "#define GIT_SOURCE_DESC \"`echo_git_info``date`\""

The above will result in the the variable GIT_SOURCE_DESC being defined in the file git_info.h. Thus, in your main.cpp you can then:

#include <iostream>
#include "ginkgo_info.h"

int main(int argc, char* argv[]) {
    std::cout << "Source is: " << $GIT_SOURCE_DESC << std::endl;
}

这篇关于在c ++二进制文件中嵌入git描述字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆