gcc条件编译 [英] gcc conditional compilation
问题描述
我正在学习条件编译,我认为迄今为止我已经很了解它。现在,如果我有代码:
#ifdef UMP_TO_FILE
//在这里做些什么...
#endif
我运行:
gcc myprogram.c -DUMP_TO_FILE
然后,代码块//在这里做点什么......被编译。现在,我的问题是:
什么是-DUMP_TO_FILE标志?
我认为标志是-D,它定义了宏UMP_TO_FILE,但我想确定语法,gcc --help不会告诉我任何关于此的信息,也许我不知道如何在互联网!!
非常感谢您分享您的知识!
man gcc
的输出包含以下小片段:
-D name
预定义
名称
作为一个宏,定义为1。 b
$ b-D name = definition
定义的内容被标记并处理,就好像它们出现在翻译第三阶段在
#define
指令中。特别是,定义将被嵌入的换行符截断。
如果您是从一个shell或类似shell的程序中调用预处理程序,那么您可能需要使用shell的引用语法来保护字符,比如在shell语法中具有含义的空格。
如果您希望在命令行中定义类似函数的宏,请将其参数列表在等号之前加上括号(如果有的话)。圆括号对大多数炮弹都有意义,因此您需要引用该选项。用
sh
和csh
,-D'name(args ...)= definition'
工作。
-D
和-U
选项按照它们在命令行上的顺序进行处理。所有-imacros
文件和-include
文件选项在全部-D
和-U
选项。
所以,选项 -DUMP_TO_FILE
相当于在你的源代码中放置 #define UMP_TO_FILE 1
这个行(有细微的差别,但是没有必要在这里覆盖他们)。
顺便说一下,我认为这是相当糟糕的做法。作者正在执行诡计,因此它看起来像编译器选项 DUMP_TO_FILE
,而不是预处理器宏。这很聪明,但是对于那些想知道 UMP_TO_FILE
实际上意味着什么的人来说,这会造成麻烦。
就我个人而言,我更愿意看到 -DDUMP_TO_FILE
和 #ifdef DUMP_TO_FILE
,因为它意图更清晰。
I'm learning about conditional compilation and I think that I understand it well so far. Now, if I have the code:
#ifdef UMP_TO_FILE
//do something here...
#endif
and I run:
gcc myprogram.c -DUMP_TO_FILE
Then, the code block "//do something here..." gets compiled. Now, my question is:
What exactly the -DUMP_TO_FILE flag does?
I think that the flag is "-D" and it defines the macro "UMP_TO_FILE", but I want to be sure of the syntaxis and "gcc --help" does not tell me anything about this and maybe I don't know how to search for this on the Internet!!
Thank you very much for sharing your knowledge!
The output of man gcc
contains this little snippet:
-D name
Predefine
name
as a macro, with definition 1.-D name=definition
The contents of definition are tokenized and processed as if they appeared during translation phase three in a
#define
directive. In particular, the definition will be truncated by embedded newline characters.If you are invoking the preprocessor from a shell or shell-like program you may need to use the shell's quoting syntax to protect characters such as spaces that have a meaning in the shell syntax.
If you wish to define a function-like macro on the command line, write its argument list with surrounding parentheses before the equals sign (if any). Parentheses are meaningful to most shells, so you will need to quote the option. With
sh
andcsh
,-D'name(args...)=definition'
works.
-D
and-U
options are processed in the order they are given on the command line. All-imacros
file and-include
file options are processed after all-D
and-U
options.
So, the option -DUMP_TO_FILE
is equivalent to putting the line #define UMP_TO_FILE 1
in your source code (there are subtle differences but there's no need to cover them here).
As an aside, I consider this rather bad practice. The author is performing trickery so that it looks like a compiler option DUMP_TO_FILE
rather than a pre-processor macro. That's sort of clever but it's going to cause trouble for anyone looking at the source wondering what UMP_TO_FILE
actually means.
Personally, I'd rather see -DDUMP_TO_FILE
and #ifdef DUMP_TO_FILE
since it's much clearer in intent.
这篇关于gcc条件编译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!