QT DEFINES 是否与 C++ 中的 #define 做同样的事情?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16244040/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is the QT DEFINES doing the same thing as #define in C++?
提问by Terence Chow
What does the DEFINES += includthisvariable
do in QT for a .pro
file?
什么是DEFINES += includthisvariable
在QT一个做.pro
文件?
If it works like the #define
in C++, where is includethisvariable
defined so that the preprocessor can replace includethisvariable
with the value I set?
如果它像#define
在 C++ 中一样工作,那么在哪里includethisvariable
定义以便预处理器可以用includethisvariable
我设置的值替换?
I understand what #define
does in c++ because you set the value beside what you define. However here it seems like you just list a name...The QT docs didn't help explain this for me.
我了解#define
C++ 中的作用,因为您将值设置在您定义的值旁边。但是在这里,您似乎只是列出了一个名称……QT 文档并没有帮助我解释这一点。
回答by Michael Burr
The items in the Qt Project file's DEFINES
variable end up on the compiler's command line with the -D
option (or whatever is appropriate for the compiler being used). To give your macro definition a value instead of merely defining it, use the following:
Qt 项目文件DEFINES
变量中的项目以-D
选项(或任何适合正在使用的编译器的方式)在编译器的命令行中结束。要为您的宏定义提供一个值而不是仅仅定义它,请使用以下命令:
DEFINES += FOOBAR=foobar_value
That will show up on the compiler's command line as -DFOOBAR=foobar_value
这将在编译器的命令行上显示为 -DFOOBAR=foobar_value
If you need spaces you need to quote the value - and escape the quotes that'll be passed on the compiler command line:
如果需要空格,则需要引用该值 - 并转义将在编译器命令行上传递的引号:
DEFINES += FOOBAR="\"foobar value\""
This one shows up as: -DFOOBAR="foobar value"
这个显示为: -DFOOBAR="foobar value"
回答by suspectus
Yes it works in the same way. DEFINES += includethisvariable
includes the pre-processor symbol includethisvariable
in the sources being compiled.
是的,它以相同的方式工作。在正在编译的源DEFINES += includethisvariable
代码includethisvariable
中包含预处理器符号。
This means any #ifdef statements like
这意味着任何 #ifdef 语句,如
#ifdef includethisvariable
...
...
#endif
are included in the source being compiled.
包含在正在编译的源代码中。
Macros with values can also be defined
也可以定义带有值的宏
`DEFINES += "MAXBUFFERSIZE=4096"
回答by Bar?? Akkurt
From the documentation:
从文档:
The defines are specified in the .config file. The .config file is a regular C++ file, prepended to all your source files when they are parsed. Only use the .config file to add lines as in the example below:
定义在 .config 文件中指定。.config 文件是一个常规的 C++ 文件,在解析时添加到所有源文件中。仅使用 .config 文件添加行,如下例所示:
#define NAME value
回答by Adam Zahran
If you'd like to define a macro of a string literal in your qmake file, equivalent to #define VAR "Some string"
如果你想在你的 qmake 文件中定义一个字符串文字的宏,相当于 #define VAR "Some string"
It's gonna look like this:
它看起来像这样:
DEFINES += CODE_WORKING_DIR=\\"$$PWD\\"
So that it would produce that as an argument to g++ (or whatever you're using to compile):
这样它就会产生它作为 g++ 的参数(或者你用来编译的任何东西):
g++ -c -DCODE_WORKING_DIR=\"/path/to/my/code\"
It is ugly. If someone knows a better way, please let me know.
它很丑。如果有人知道更好的方法,请告诉我。