在程式內動態獲取svn版本號

2021-06-07 01:48:20 字數 985 閱讀 8471

生產線上使用的程式往往比當前正在開發的**庫舊,當程式出現bug時,為了可以追蹤到程式對應的版本,需要在程式內記錄**庫對應的版本號。本文說明怎麼在程式內動態獲取svn版本號。

1,設定環境變數(可選)

shell**  

export releaseversion="\\\"1.2\\\""

export buildos="\\\"`uname`\\\""

export builddate="\\\"`date +%y%m%d`\\\""

2,在makefile定義變數(可選)

shell**  

-dreleaseversion=$(releaseversion) -dbuildos=$(buildos) -dbuilddate=$(builddate)  

3,在c++**中通過$id獲取svn版本號

c++**  

static

const

char g_prevision __attribute__((used)) = ;  

4,對svnversion.cpp設定svn屬性

svn**  

svn propset svn:keywords "id" svnversion.cpp  

svn提交後,g_prevision 自動變成

c++**  

static

const

char g_prevision __attribute__((used)) = ;  

可以獲得svn最後變更的日期、版本號及使用者名稱

5,使用例子:在c++**中生成程式版本號

c++**  

void getversion(void)    

關鍵點:

__attribute__((used))不能去掉,否則 gcc 看到這個變數沒人使用,會進行優化,從而去掉。

更多參考:

gcc attribute:

svn 關鍵字替換:

獲取程式版本號

取程序版本號碼 vc 2008 控制台程式的版本資源需要手動插入 include stdafx.h include pragma comment lib,version.lib dword getver word wver1,word wver2,word wver3,word wver4 int ...

MFC 獲取程式的版本號

1.引入version.lib pragma comment lib,version.lib 因為我們需要用到verqueryvalue,這一步不可缺少。2.新增 char infobuf new char infosize getfileversioninfo cpath,0,infosize,i...

Swift 獲取版本號

swift版本 let infodictionary bundle.main.infodictionary if let infodictionary infodictionaryoc版本 nsdictionary infodictionary nsbundle mainbundle infodic...