Add a define to qmake WITH a value?

DEFINES += "WINVER=0x0500" works for me.

This way, -DWINVER=0x0500 is added to the command line of the compiler, which is the syntax GCC/mingw expects for command line preprocessor definitions (see here for the details).


DEFINES += MY_DEF=\\\"String\\\"

This format is to be used when one intends to have the macro replaced by string element


As an addendum, if you want to execute shell code instead of just setting a constant (e.g. for getting a version number or a date):

Either use $$system(). This is run when qmake is executed:

DEFINES += GIT_VERSION=$$system(git describe --always)

Or use $() if the code should be run on every build (i.e. when the makefile is executed). For DEFINES you need to escape the command if it contains spaces, otherwise qmake inserts unwanted -D's:

DEFINES += GIT_VERSION='$(shell git describe --always)'

This will then be copied literally into the makefile.

If the command's output contains spaces, you need another layer of escapement (this time for make):

DEFINES += BUILD_DATE='"$(shell date)"'

If you need quotes around your value to get a string, it gets a bit ugly:

DEFINES += BUILD_DATE='"\\\"$(shell date)\\\""'

I would recommend to use the preprocessors stringify operation in this case:

#define _STR(x) #x
#define STRINGIFY(x)  _STR(x)

printf("this was built on " STRINGIFY(BUILD_DATE) "\n");

Tags:

Qt

Qmake