Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is the QT DEFINES doing the same thing as #define in C++?

Tags:

c++

qt

What does the DEFINES += includthisvariable do in QT for a .pro file?

If it works like the #define in C++, where is includethisvariable defined so that the preprocessor can replace includethisvariable with the value I set?

I understand what #define does in c++ because you set the value beside what you define. However here it seems like you just list a name...The QT docs didn't help explain this for me.

like image 457
Terence Chow Avatar asked Apr 26 '13 19:04

Terence Chow


4 Answers

The items in the Qt Project file's DEFINES variable end up on the compiler's command line with the -D option (or whatever is appropriate for the compiler being used). To give your macro definition a value instead of merely defining it, use the following:

DEFINES += FOOBAR=foobar_value

That will show up on the compiler's command line as -DFOOBAR=foobar_value

If you need spaces you need to quote the value - and escape the quotes that'll be passed on the compiler command line:

DEFINES += FOOBAR="\"foobar value\""

This one shows up as: -DFOOBAR="foobar value"

like image 159
Michael Burr Avatar answered Oct 26 '22 19:10

Michael Burr


Yes it works in the same way. DEFINES += includethisvariable includes the pre-processor symbol includethisvariable in the sources being compiled.

This means any #ifdef statements like

#ifdef includethisvariable
...
...
#endif

are included in the source being compiled.

Macros with values can also be defined

`DEFINES += "MAXBUFFERSIZE=4096"
like image 6
suspectus Avatar answered Oct 26 '22 19:10

suspectus


If you'd like to define a macro of a string literal in your qmake file, equivalent to #define VAR "Some string"

It's gonna look like this:

DEFINES += CODE_WORKING_DIR=\\\"$$PWD\\\"

So that it would produce that as an argument to g++ (or whatever you're using to compile):

g++ -c -DCODE_WORKING_DIR=\"/path/to/my/code\"

It is ugly. If someone knows a better way, please let me know.

like image 2
Adham Zahran Avatar answered Oct 26 '22 18:10

Adham Zahran


From the documentation:

The defines are specified in the .config file. The .config file is a regular C++ file, prepended to all your source files when they are parsed. Only use the .config file to add lines as in the example below:

#define NAME value
like image 1
Barış Akkurt Avatar answered Oct 26 '22 19:10

Barış Akkurt