C 预处理器在扩展命令行中定义的宏后添加尾随“1”
C preprocessor adds trailing "1" after expanding a macro defined in a command line
$ echo 'MACRO();' | gcc -D'MACRO() hello' -E -P -
hello 1;
^ why 1?!!!!
你知道为什么扩展后的宏后面要加“1”吗?
来自GCC preprocessor documentation:
-D name
Predefine name as a macro, with definition 1.
-D name=definition
Predefine name as a macro, with definition definition. The contents of definition are tokenized and processed as if they appeared during translation phase three in a #define directive.
所以你想要:
$ echo 'MACRO();' | gcc -D'MACRO()=hello' -E -P -
hello;
注意定义中的等号。
至于为什么没有它它会做什么:空格是预处理器符号定义中的分隔符。所以预处理器将 "hello" 分配给符号。然后它注意到定义中没有等号,所以它也附加了默认的“1”。这不是错误,只是 space "in" 预处理器符号的情况。
$ echo 'MACRO();' | gcc -D'MACRO() hello=foo' -E -P -
hello foo;
$ echo 'MACRO();' | gcc -D'MACRO() hello' -E -P -
hello 1;
^ why 1?!!!!
你知道为什么扩展后的宏后面要加“1”吗?
来自GCC preprocessor documentation:
-D name
Predefine name as a macro, with definition 1.
-D name=definition
Predefine name as a macro, with definition definition. The contents of definition are tokenized and processed as if they appeared during translation phase three in a #define directive.
所以你想要:
$ echo 'MACRO();' | gcc -D'MACRO()=hello' -E -P -
hello;
注意定义中的等号。
至于为什么没有它它会做什么:空格是预处理器符号定义中的分隔符。所以预处理器将 "hello" 分配给符号。然后它注意到定义中没有等号,所以它也附加了默认的“1”。这不是错误,只是 space "in" 预处理器符号的情况。
$ echo 'MACRO();' | gcc -D'MACRO() hello=foo' -E -P -
hello foo;