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 do I make a global DEFINE += variable?
Forum Update on Monday, May 27th 2025

How do I make a global DEFINE += variable?

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 3 Posters 5.0k 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.
  • G Offline
    G Offline
    graniteDev
    wrote on last edited by
    #1

    I have a project with sub projects, and I was hoping I could define a variable in the top level .pro file such that any sub project could see it. For example, the tree looks like this:

    topLevel
    |-topLevel.pro
    |-subProj1
    |-subProj2
    |-subproj3

    and in topLevel.pro, I tried adding

    DEFINE += _myVar_
    

    and then in subProj2, in it's header.h i added

    #ifdef _myVar_
    //some code here
    #endif _myVar_
    

    However this didn't work, as the header.h in subProj2 couldn't see the _myVar_
    variable. If I placed the variable in subProj2.pro, this worked, however I want to be able to use the same variable globally in the other projects, so adding it multiple times is not going to work for my specif application.

    What is the best way to define a global variable to accomplish this?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      IIRC, you can put a file named .qmake.conf in your top level folder and put your common variables there.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      G 3 Replies Last reply
      1
      • SGaistS SGaist

        Hi,

        IIRC, you can put a file named .qmake.conf in your top level folder and put your common variables there.

        G Offline
        G Offline
        graniteDev
        wrote on last edited by
        #3

        @SGaist

        Ok so I added DEFINE += _myVar_ to the .qmake.conf file that we already have in the project.

        The code is still grayed out between the #indef and #endif statements. Is it possible to tell through Qt Creator if this has worked, or is this a compile time thing only?

        1 Reply Last reply
        0
        • SGaistS SGaist

          Hi,

          IIRC, you can put a file named .qmake.conf in your top level folder and put your common variables there.

          G Offline
          G Offline
          graniteDev
          wrote on last edited by
          #4

          @SGaist

          Ok, that doesn't appear to work. It did not recognize the variable and thus I got a bunch of compile errors because the code between the #ifdef and #endif did not compile.

          1 Reply Last reply
          0
          • SGaistS SGaist

            Hi,

            IIRC, you can put a file named .qmake.conf in your top level folder and put your common variables there.

            G Offline
            G Offline
            graniteDev
            wrote on last edited by
            #5

            @SGaist

            Thank you, you gave me a good reference to search on. I ended up making a .pri file in the top level directory. Then, any subdirectory that needs the variables, I added

            include(..\..\myVar.pri)
            

            to it's .pro file. This worked. I'm not thrilled with it, since it adds another file to the project and it's not clear to someone following behind me exactly why so I'll continue looking for another solution, but it's a solution.

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              You may have to re-run qmake in order for it to be taken into account.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              Q 1 Reply Last reply
              0
              • SGaistS SGaist

                You may have to re-run qmake in order for it to be taken into account.

                Q Offline
                Q Offline
                QtTester
                wrote on last edited by QtTester
                #7

                @SGaist Hi, my dir level and qmake.conf look like this:

                variable.pri
                multipro
                --multipro.pro
                --.qmake.conf
                subpro1
                --subpro1.pro
                subpro2
                --subpro2.pro
                

                if I put available in .qmake.conf, multipro.pro can see it, but subpro1/2.pro still cannot see it.
                I don't want to use variable.pri because someone has no permission to access it.
                So how to put variable in multipro but can be seen in subpro1/2.pro?

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  Hi,

                  That file should be one level above.

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  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