向 qmake 添加一个带有值的定义?
如何使用带有值的 qmake 添加定义:
例如,这在我的 .pro 文件中不起作用(如我所料):
DEFINES += WINVER 0x0500
也不
DEFINES += "WINVER 0x0500"
如何在任何内容开始编译之前将 WINVER 定义为 0x0500,以便它的定义不会受到任何影响通过编译或包含顺序的方式?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
DEFINES += "WINVER=0x0500"
对我有用。这样,
-DWINVER=0x0500
就被添加到编译器的命令行中,这是 GCC/mingw 期望的命令行预处理器定义语法(请参阅 此处 了解详细信息)。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).当打算将宏替换为字符串元素时,将使用此格式
This format is to be used when one intends to have the macro replaced by string element
作为附录,如果您想执行 shell 代码而不仅仅是设置常量(例如获取版本号或日期):
请使用
$$system()
。这是执行qmake时运行:或者如果代码应该在每个构建上运行(即当执行makefile时),则使用
$()
)。对于
DEFINES
,如果命令包含空格,则需要对其进行转义,否则 qmake 会插入不需要的-D
:然后这将被逐字复制到 makefile 中。
如果命令的输出包含空格,则需要另一层擒纵(这次是 make):
如果您需要在值周围加上引号来获取字符串,则它会变得有点难看:
在这种情况下,我建议使用预处理器 stringify 操作:
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: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: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):
If you need quotes around your value to get a string, it gets a bit ugly:
I would recommend to use the preprocessors stringify operation in this case:
#define STRING "Value with space" fro Qt *.PRO file :
为了从 QT 项目文件添加#define STRING "Value with space",我们必须write :
写入 Makefile.Release 文件:
总之,在该行上:
DEFINES += "VERSION_LOG=\"\\\"Version 2.5.1\\\"\""
第一个最后一个
"
告诉 QMake 将整个句子作为字符串读取第一个和最后一个
\"
将第一个和最后一个"
写入-DVERSION_LOG="\"Version 2.5.1\""
第一个和最后一个
\\\"
写入\
,然后写入" 到
-DVERSION_LOG="\"版本 2.5.1\""
#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 :
which gives into the Makefile.Release file :
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 stringThe 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 的答案在 .pro 文件中运行良好。然而,当从命令行调用 qmake 时,我不得不去掉空格,即使用某物。像下面这样,进行定义工作:
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 :
如果你想定义一个在 Objective-C 中使用的字符串文字,那么你需要记住转义引号之前的 @
If you want to define a string literal for use in Objective-C then you need to remember the @ before the escaped quotes
如果您在 bash 脚本中运行 qmake 并希望使用 bash 脚本中的版本(semver)作为 QString 变量,您可以执行以下操作:
在 bash 中:
在 .cpp 中:
If you run qmake in bash script and want to use VERSION (semver) from bash script as QString variable, you can do:
In bash:
In .cpp: