向 qmake 添加一个带有值的定义?

发布于 2024-09-11 12:07:32 字数 247 浏览 7 评论 0 原文

如何使用带有值的 qmake 添加定义:

例如,这在我的 .pro 文件中不起作用(如我所料):

DEFINES += WINVER 0x0500

也不

DEFINES += "WINVER 0x0500"

如何在任何内容开始编译之前将 WINVER 定义为 0x0500,以便它的定义不会受到任何影响通过编译或包含顺序的方式?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

白龙吟 2024-09-18 12:07:32

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

凯凯我们等你回来 2024-09-18 12:07:32
DEFINES += MY_DEF=\\\"String\\\"

当打算将宏替换为字符串元素时,将使用此格式

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

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

橘和柠 2024-09-18 12:07:32

作为附录,如果您想执行 shell 代码而不仅仅是设置常量(例如获取版本号或日期):

请使用 $$system()。这是执行qmake时运行:

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

或者如果代码应该在每个构建上运行(即当执行makefile时),则使用$() )。
对于DEFINES,如果命令包含空格,则需要对其进行转义,否则 qmake 会插入不需要的-D

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

然后这将被逐字复制到 makefile 中。

如果命令的输出包含空格,则需要另一层擒纵(这次是 make):

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

如果您需要在值周围加上引号来获取字符串,则它会变得有点难看:

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

在这种情况下,我建议使用预处理器 stringify 操作:

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

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

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");
没有伤那来痛 2024-09-18 12:07:32

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

为了从 QT 项目文件添加#define STRING "Value with space",我们必须write :

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

写入 Makefile.Release 文件:

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

总之,在该行上: 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 :

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

葬﹪忆之殇 2024-09-18 12:07:32

Greg 的答案在 .pro 文件中运行良好。然而,当从命令行调用 qmake 时,我不得不去掉空格,即使用某物。像下面这样,进行定义工作:

qmake DEFINES+="WINVER 0x0500"

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"
新一帅帅 2024-09-18 12:07:32

如果你想定义一个在 Objective-C 中使用的字符串文字,那么你需要记住转义引号之前的 @

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

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\\"'
夜无邪 2024-09-18 12:07:32

如果您在 bash 脚本中运行 qmake 并希望使用 bash 脚本中的版本(semver)作为 QString 变量,您可以执行以下操作:

在 bash 中:

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

在 .cpp 中:

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

QString version = QString(STRINGIFY(VERSION))

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))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文