Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

qmake set GCC string variable



  • Hi guys,

    I'm trying to assign a string value to a variable at compile time. Trying and googling about 2 days but no lack.

    Here's how I'm trying to do that.
    Below are in .pro file.

    CHANGESET_FILE_PATH = $$absolute_path("changeset.txt", $$PWD/..)
    CHANGESET = $$cat($$CHANGESET_FILE_PATH)
    QMAKE_CXXFLAGS += -DCHANGESET="$$CHANGESET"
    message("Changeset: "$$CHANGESET)

    This is qmake output;
    Project MESSAGE: Changeset: "045c99e9e492"

    And I'm trying to access CHANGESET in main.cpp like that;
    qWarning() << "CHANGESET:" << CHANGESET;

    Here is compiler error;
    <command-line>:-1: error: unable to find numeric literal operator 'operator"" c99e9e492'

    Any comment?


  • Lifetime Qt Champion

    Hi,

    You should rather use the DEFINES variable. It also requires a bit more escaping.

    CHANGESETSTR = '\\"$ ${CHANGESET}\\"'
    DEFINES += CHANGESET=\"$ ${CHANGESETSTR}\"
    

    Remove the space between the dollars, it's only so that the forum doesn't hide them



  • Thank you very much.

    I did not tried your solution but find another one.

    This is how I declared variable at compile time;
    QMAKE_CXXFLAGS += -DCHANGESET="$$CHANGESET"

    And this is how I stringified variable in main.cpp;
    #define STRINGIZE(x) #x
    #define STRINGIZE_VALUE_OF(x) STRINGIZE(x)
    int main(int argc, char *argv[])
    {
    qWarning() << "CHANGESET:" << STRINGIZE_VALUE_OF(CHANGESET);
    }

    I'll try your solution asap and let you know the result.
    Thanks again.


Log in to reply