Add a define to qmake WITH a value?

做~自己de王妃 提交于 2019-11-27 03:53:08

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

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

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

iliis

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");

#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\""

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"

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\\"'
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!