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

How to use macro definitions defined in another project's pro file?



  • Hello everyone,
    I have a question. Could somebody give me some answers?

    When I was learning QT, I found out that I could define macro definitions in the pro file of a project. Then I want do use the definitions in another project directly or indirectly. But unfortunately it pops up some errors when I compile the project that uses the definations.

    DEFINES += LIB_VERSION=\\\"$$VERSION\\\"
    

    The error is that it can't find the definitions.

    But if I define the definitions in a header file, it works.

    So could someone tell me the reason, please? And could somebody tell me can I use macro definitions defined in another project's pro file? If I can, how ?

    Thanks in advance.


  • Moderators

    @qingshui-kong said in How to use macro definitions defined in another project's pro file?:

    And could somebody tell me can I use macro definitions defined in another project's pro file? If I can, how ?

    Move the definitions to a .pri file.

    Then in every .pro you need to use them, do this:

    include(path/to/your/file.pri)
    

    And it will work.

    The error is that it can't find the definitions.
    But if I define the definitions in a header file, it works.
    So could someone tell me the reason, please?

    Different .pro is a different project, so qmake and your compiler have no way of knowing them. They don't do a search of all files on your system, only strictly the files mentioned in project file.



  • @sierdzio
    Thank you very much!
    I will have a try.
    That works.

    However, when I try to use it, I encounter another problem.
    Actually, I want to use it to get versions of the application and all the libraries. But the versions for each libraries and application may be different.
    I use

    VERSION = X.X.X.X
    

    for each libraries and application. But if I use

    DEFINES += LIBA_VERSION=\\\"$$VERSION\\\"
    DEFINES += LIBB_VERSION=\\\"$$VERSION\\\"
    DEFINES += APP_VERSION=\\\"$$VERSION\\\"
    

    LIBA_VERSION, LIBB_VERSION and APP_VERSION will return the same version when I use them to achieve the versions of each libraries and application.
    To solve it, what should I do?
    And are there any other ways to set and get versions for application and each libraries?


  • Moderators

    @qingshui-kong said in How to use macro definitions defined in another project's pro file?:

    And are there any other ways to set and get versions for application and each libraries?

    Each library should have a separate version. If you need to know version of libA or libB in your application, then add a function or define or macro in your libs that you app will be able to read. For example:

    // Somewhere in library A
    #define LIB_A_VERSION 1.2.3
    
    // In application:
    QString libAVersion(LIB_A_VERSION);
    

    Here is how Qt itself defines this: https://code.woboq.org/qt5/qtbase/src/corelib/global/qglobal.h.html#_M/QT_VERSION



  • @sierdzio
    OK, thanks a lot.

    If you need to know version of libA or libB in your application, then add a function or define or macro in your libs that you app will be able to read.

    Does it mean I can't use VERSION in pro file and I can't define macro definitions in pri file?


  • Moderators

    @qingshui-kong said in How to use macro definitions defined in another project's pro file?:

    @sierdzio
    OK, thanks a lot.

    If you need to know version of libA or libB in your application, then add a function or define or macro in your libs that you app will be able to read.

    Does it mean I can't use VERSION in pro file and I can't define macro definitions in pri file?

    You can and should use VERSION in .pro. You can also use it in .pri if you like, but not in the way you do now. Treat the .pri as something that is compiled for a SINGLE target, not for all libs and apps, even if you do reuse it in all these projects.



  • @sierdzio
    I see. I shouldn't use the macro to get the version in another project.


Log in to reply