Congratulations to our 2022 Qt Champions!

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:


    and in, 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, 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


    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


    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.

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


    if I put available in .qmake.conf, can see it, but subpro1/ 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/

  • Lifetime Qt Champion


    That file should be one level above.

Log in to reply