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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 20:10:19  来源:igfitidea点击:

Is the QT DEFINES doing the same thing as #define in C++?

c++qt

提问by Terence Chow

What does the DEFINES += includthisvariabledo in QT for a .profile?

什么是DEFINES += includthisvariable在QT一个做.pro文件?

If it works like the #definein C++, where is includethisvariabledefined so that the preprocessor can replace includethisvariablewith the value I set?

如果它像#define在 C++ 中一样工作,那么在哪里includethisvariable定义以便预处理器可以用includethisvariable我设置的值替换?

I understand what #definedoes 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.

我了解#defineC++ 中的作用,因为您将值设置在您定义的值旁边。但是在这里,您似乎只是列出了一个名称……QT 文档并没有帮助我解释这一点。

回答by Michael Burr

The items in the Qt Project file's DEFINESvariable end up on the compiler's command line with the -Doption (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 += includethisvariableincludes the pre-processor symbol includethisvariablein 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.

它很丑。如果有人知道更好的方法,请告诉我。