PDA

View Full Version : qmake generate user-defined header files, or pass variable definitions to program



banjoman
9th December 2010, 18:51
Hi, I'm trying to figure something out and have looked everywhere but don't see it referred to, or admittedly I could be using the wrong search term.

I want to set a variable that is to be used by both qmake and my program. So far I have only figured out that I could put that variable in both my .pro file and my .h file, the first for qmake, and the second for my program's use. Is there either a way for my program to use variables set in the pro file(s), or if no such thing exists, is there a way for me to generate, on the fly, an automatically created header file that will be created with that variable/value as a #define? If I could do the first I would be very happy, but I would be gratified to hear that I could at least do the second.

I was looking around and found that I can call qmake -set <variable> <value>, and I got really excited that I had found the thing. The web page even said that QSettings::value("<variable>") would be used to get the value, but I am now of the impression that's only for setting a global variable for the current environment, and not one to be passed in to the settings for my program. Also, that would be a command line thing that would be done separate to a regular qmake call that created Makefiles.

Does anyone have any suggestions for me to not have to create two instances of the same variable in two different files, a pro file and a h file?

Thanks...

high_flyer
9th December 2010, 19:14
Its sounds to me that what you want (which I don't really fully understand) is bad, and should not be done.
Try first explaining what it is you wish to achieve, we will probably be able to point you in the correct direction then.
What it is you want to parametrize?
Are you sure that what you want are not application arguments?

wysota
9th December 2010, 20:28
What's wrong with using qmake's DEFINES variable?

banjoman
10th December 2010, 14:02
Great idea. I hadn't realized I could pass a value along with a define given in a .pro file. I googled and found someone doing it (http://www.qtcentre.org/threads/29104-QMake-DEFINES), so I created a test project called tmp. When I compiled I got an error. My files follow:

-------- tmp.pro -------
TEMPLATE = app
TARGET =
DEPENDPATH += .
INCLUDEPATH += .
SOURCES += main.cpp
DEFINES += "MYVAR=\"myvarvalue\""

-------- resulting Makefile entry -----
DEFINES = -DMYVAR="myvarvalue" -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED

-------- main.cpp ------
#include <QApplication>
#include <QDebug>
#define S "Smellypants"
int main(int argc, char **argv) {
QApplication a(argc, argv);
qDebug() << S;
#ifdef MYVAR
qDebug() << "MYVAR is defined as: " << MYVAR;
#endif
return a.exec();
}

-------- compilation output -------
g++ -c -pipe -O2 -Wall -W -D_REENTRANT -DMYVAR="myvarvalue" -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I../../../qtsdk-2010.05/qt/mkspecs/linux-g++ -I. -I../../../qtsdk-2010.05/qt/include/QtCore -I../../../qtsdk-2010.05/qt/include/QtGui -I../../../qtsdk-2010.05/qt/include -I. -I. -o main.o main.cpp
main.cpp: In function ‘int main(int, char**)’:
main.cpp:11: error: ‘myvarvalue’ was not declared in this scope
make: *** [main.o] Error 1

-----------------------------------

Basically, I created a local #define in my main.cpp, and was able to print that string just fine (with the MYVAR print commented out). But when I tried to print MYVAR, it acts like as if the value (myvarvalue) is a code token rather than a variable value. I am not sure what the difference would be between accessing a local #define and accessing a -D define from the Makefile. I've never done this before. Any suggestions? I attached my full Makefile (as Makefile.txt) if you need to look at that.

wysota
10th December 2010, 14:43
Your MYVAR is not a string literal. Make it a string literal (there are a couple of ways to do that, e.g. use the "#" preprocessor directive) and it will work.

banjoman
10th December 2010, 14:58
Great. I am not sure how I would use the # directive from a .pro file, but I changed my DEFINES to be this:

DEFINES += MYVAR=\"\\\"myvarvalue\\\"\"

and it worked perfectly. If you would, could you let me know what you meant by using the # directive from the .pro file?

wysota
10th December 2010, 15:17
Great. I am not sure how I would use the # directive from a .pro file
Not from the .pro file. The following should work.


#define str(x) #x
qDebug() << "MYVAR is defined as: " << str(MYVAR);

banjoman
10th December 2010, 16:20
Hmmm, much cleaner than what I had done. Thanks for the clarification.

Added after 12 minutes:

Oops, spoke too soon. Just tested that and it prints:

MYVAR is defined as: MYVAR

I tried different variations of the tmp.pro definition, but they all do this, which seems to mean that the directive

#define str(x) #x

is not working correctly. Now I'm back to my uglier but working version. Are you sure this is working for you?

wysota
10th December 2010, 16:47
It should probably be:

#define str_(x) #x
#define str(x) str_(x)
qDebug() << "MYVAR is defined as: " << str(MYVAR);

banjoman
10th December 2010, 17:18
Eureka, that's it!!! Thanks!

artm
6th April 2011, 11:05
the following worked for me:

(in .pro)


VERSION = 3-alpha-10.2
DEFINES += FOO_VERSION=\\\"$$VERSION\\\"


(I need the qmake VERSION variable in the project file because I use it elsewhere,
FOO_ prefix of the define is to avoid clashing with something).

(in .cpp)


QLabel * version = new QLabel( FOO_VERSION );


The label appears to be correct ("3-alpha-10.2") in the GUI. The tripple backslashes are necessary so that it becomes \" in the Makefile and then just " when passed to the compiler by the shell.