Important: Please read the Qt Code of Conduct -

using Git version number in app

  • Hi all -

    I'm trying to automate the versioning of my application by using the value stored in a git tag. I found this article, which looks like a good start, but isn't working for me.

    Here's what I've tried (from the article): in my project file, I've added this:

    GIT_VERSION = $$system(git --git-dir="C:/wifibutton"/.git --work-tree="C:/wifibutton" describe --always --tags)

    If I'm reading the article correctly, APP_VERSION should be available to me in my c++ code. But when I try this:

        QString version(APP_VERSION);

    I get an "expected expression" error, which suggests to me that APP_VERSION isn't finding its way into my code space. Can someone tell me what I'm doing wrong?


  • Lifetime Qt Champion


    Getting a string in a define on the command line can be pretty tricky because of the quote escaping you have to do.

    One simpler thing you can do is to generate a header from a template.

  • Hi SGaist - can you please elaborate?

  • Lifetime Qt Champion

    You have a nice example here.

    Basically, you have a header template which contains the variables you want to substitute and in your build folder you'll get the header you want with the content you wanted.

  • Very, very interesting. Never knew about that QMAKE_SUBSTITUTES.

    In my project file:

    GIT_VERSION = $$system(git --git-dir="C:/wifibutton"/.git --work-tree="C:/wifibutton" describe --always --tags)
        // this is to remove characters that could prevent compilation.
        GIT_VERSION ~= s/-\d+-g[a-f0-9]{6,} 


    #ifndef CONFIG_H_IN
    #define CONFIG_H_IN
    #define APP_VERSION '"$$GIT_VERSION"'
    #endif // CONFIG_H_IN

    and in my source code:

        QString version(APP_VERSION);
        // etc.


Log in to reply