Qt add Git version or SHA1

紀錄一下如何在 Qt .pro 專案檔裡帶入 git 的版本資訊或是 git 的 SHA1,讓編譯出來的 Qt 程式夾帶 git 版本資訊。

Add code to Qt .pro file.

1
2
GIT_VERSION = "$(shell git -C \""$$_PRO_FILE_PWD_"\" describe)"
DEFINES += GIT_VERSION=\\\"$$GIT_VERSION\\\"

(Windows 下要先確保 git 指令的路徑已經有加到系統環境變數裡)

Use the define inside your code. (cpp files)

1
QString version(GIT_VERSION);

參考文章
https://stackoverflow.com/questions/27041573/print-git-hash-in-qt-as-macro-created-at-compile-time
https://gist.github.com/grassator/11405930
https://www.everythingfrontend.com/posts/app-version-from-git-tag-in-qt-qml.html

其它相關文章推薦
[Qt] 讀檔,讀取 txt 文字檔
[Qt] 寫檔,寫入 txt 文字檔
安裝 Qt 在 Windows 7 (使用MSVC)
Qt產生的exe發布方式
Qt 新增多國語言.ts翻譯檔案
Qt5的中文亂碼問題如何解決