How do I make a global DEFINE += variable?



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


  • Lifetime Qt Champion

    Hi,

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



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



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



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


  • Lifetime Qt Champion

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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.