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
What does the g++ -D flag do?
提问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 1
in 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 #ifdef
statements 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 -D
flag. 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.
在编译文件的开头。