Linux g++ -D 标志有什么作用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10337248/
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-06 06:00:40  来源:igfitidea点击:

What does the g++ -D flag do?

linuxcompilationg++flags

提问by Sterling

I am looking at a CFLAGS of -

我在看一个 CFLAGS -

CFLAGS=-g -w -D LINUX -O3 -fpermissive

in a Makefile. What does the -D flag do? I see on the man page that

在 Makefile 中。-D 标志有什么作用?我在手册页上看到

-D name
    Predefine name as a macro, with definition 1. 

but I don't know how to interpret that. My interpretation is...its making LINUX a macro and only doing -03 and -fpermissive when in a linux environment. Is that right? If not, then what? Thanks for any help

但我不知道如何解释。我的解释是......它使 LINUX 成为一个宏,并且只在 linux 环境中执行 -03 和 -fpermissive。那正确吗?如果不是,那又怎样?谢谢你的帮助

采纳答案by Gabriel Southern

It is equivalent to adding the statement #define LINUX 1in the source code of the file that is being compiled. It does not have any effect on other compilation flags. The reason for this is it's an easy way to enable #ifdefstatements in the code. So you can have code that says:

相当于#define LINUX 1在正在编译的文件的源代码中添加语句。它对其他编译标志没有任何影响。这样做的原因是它是一种#ifdef在代码中启用语句的简单方法。所以你可以有这样的代码:

#ifdef LINUX
   foo;
#endif

It will only be enabled if that macro is enabled which you can control with the -Dflag. So it is an easy way to enable/disable conditional compilation statements at compile time without editing the source file.

只有当您可以使用-D标志控制的宏被启用时,它才会被启用。因此,这是一种在编译时启用/禁用条件编译语句而无需编辑源文件的简单方法。

回答by jpalecek

It doesn't have anything to do with -O3. Basically, it means the same as

与 没有任何关系-O3。基本上,它的意思与

#define LINUX 1

at the beginning of the compiled file.

在编译文件的开头。