16

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.

Terence Chow
  • 9,907
  • 20
  • 69
  • 130
  • Possible duplicate of [Add a define to qmake WITH a value?](https://stackoverflow.com/questions/3348711/add-a-define-to-qmake-with-a-value) – p-a-o-l-o Jul 11 '18 at 10:06
  • Please refer to the possible duplicate: the answers here are plainly wrong about how to pass string literals to the -D option. – p-a-o-l-o Jul 11 '18 at 10:10

4 Answers4

20

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"

Michael Burr
  • 321,763
  • 49
  • 514
  • 739
6

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"
suspectus
  • 15,729
  • 8
  • 46
  • 54
2

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.

Adham Zahran
  • 1,729
  • 2
  • 16
  • 32
1

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
Barış Akkurt
  • 2,123
  • 3
  • 21
  • 37
  • Can't edit (edit queue is full, so I'll post a working link as a comment): http://doc.qt.io/qtcreator/creator-project-generic.html#specifying-defines – AntonyG May 10 '17 at 07:40