Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to use macro definitions defined in another project's pro file?

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

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 2 Posters 1.1k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Q Offline
    Q Offline
    Qingshui Kong
    wrote on last edited by
    #1

    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.

    sierdzioS 1 Reply Last reply
    0
    • Q Qingshui Kong

      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.

      sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

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

      (Z(:^

      Q 1 Reply Last reply
      4
      • sierdzioS sierdzio

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

        Q Offline
        Q Offline
        Qingshui Kong
        wrote on last edited by Qingshui Kong
        #3

        @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?

        sierdzioS 1 Reply Last reply
        0
        • Q Qingshui Kong

          @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?

          sierdzioS Offline
          sierdzioS Offline
          sierdzio
          Moderators
          wrote on last edited by sierdzio
          #4

          @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

          (Z(:^

          Q 1 Reply Last reply
          0
          • sierdzioS sierdzio

            @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

            Q Offline
            Q Offline
            Qingshui Kong
            wrote on last edited by
            #5

            @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?

            sierdzioS 1 Reply Last reply
            0
            • Q Qingshui Kong

              @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?

              sierdzioS Offline
              sierdzioS Offline
              sierdzio
              Moderators
              wrote on last edited by
              #6

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

              (Z(:^

              Q 1 Reply Last reply
              0
              • sierdzioS sierdzio

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

                Q Offline
                Q Offline
                Qingshui Kong
                wrote on last edited by
                #7

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

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved