Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/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)
    DEFINES += APP_VERSION=\\"$$GIT_VERSION\\"
    

    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?

    Thanks...


  • Lifetime Qt Champion

    Hi,

    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)
    win32
    {
        // this is to remove characters that could prevent compilation.
        GIT_VERSION ~= s/-\d+-g[a-f0-9]{6,} 
    }
    QMAKE_SUBSTITUTES += config.h.in
    

    and config.h.in:

    #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.
    

    Thanks!


Log in to reply