63

How do I add a define with qmake WITH a value:

For example, this does not work (as I expected) in my .pro file:

DEFINES += WINVER 0x0500

nor

DEFINES += "WINVER 0x0500"

How do I define WINVER as 0x0500 before anything starts compiling so it's definition is not affected in any way by compilation or include order?

7 Answers 7

81

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).

2
  • 2
    +1 This works perfectly, thanks! It added -DWINVER=0x0500 to the Makefile just as you said. PS - I didn't need the quotes since there's no spaces. ;) Jul 28, 2010 at 7:41
  • 11
    For posterity's sake, if you want to define a string value in the qmake file, it seems that you must use a rather strange combination of slashes and quotes: DEFINES += MY_DEF='\\"string\\"'
    – JCooper
    Nov 29, 2012 at 17:29
55
DEFINES += MY_DEF=\\\"String\\\"

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

30

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");
3
  • +1 for the GIT_VERSION example and notes about escaping quotes. OT but I've found the following most useful GIT_VERSION=\\\"$$system(git describe --always --abbrev=0)\\\" to get the latest tag version, or failing that the current commit Mar 22, 2017 at 14:07
  • not that the «every build» option won't work in Windows (as the build is performed via nmake, not a real make)
    – d.Candela
    Sep 10, 2019 at 12:29
  • STRINGIFY made my life a whole lot easier.
    – Andreas
    May 7, 2020 at 14:28
12

#define STRING "Value with spaces" fro Qt *.PRO file :

In order to add a #define STRING "Value with spaces" from QT Project file, we had to write :

DEFINES += "VERSION_LOG=\"\\\"Version 2.5.1\\\"\""
DEFINES += "VERSION_QT=\"\\\"Qt 5.10\\\"\""

which gives into the Makefile.Release file :

DEFINES       = -DUNICODE -D_UNICODE -DVERSION_LOG="\"Version 2.5.1\"" -DVERSION_QT="\"Qt 5.10\"" -DQT_NO_DEBUG [...]

In summary, on that line : DEFINES += "VERSION_LOG=\"\\\"Version 2.5.1\\\"\""

The first and last " tells QMake to read the entire sentence as a string

The first and last \" writes the first and last " into -DVERSION_LOG="\"Version 2.5.1\""

The first and last \\\" writes a \ then a " into -DVERSION_LOG="\"Version 2.5.1\""

0
4

Greg's answer works fine in a .pro file. However, when calling qmake from the command line, I had to leave away the spaces, i.e. use sth. like the following, to make a define work :

qmake DEFINES+="WINVER 0x0500"
1

If you want to define a string literal for use in Objective-C then you need to remember the @ before the escaped quotes

DEFINES += MY_DEF='@\\"string-literal\\"'
0

If you run qmake in bash script and want to use VERSION (semver) from bash script as QString variable, you can do:

In bash:

qmake Project.pro DEFINES+=VERSION=\"1.0.0\"

In .cpp:

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

QString version = QString(STRINGIFY(VERSION))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.