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 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?
IIRC, you can put a file named
.qmake.confin your top level folder and put your common variables there.
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?
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.
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.
You may have to re-run qmake in order for it to be taken into account.
QtTester last edited by QtTester
@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?
That file should be one level above.