C #error 宏可以显示多行消息吗?
Can a C #error macro display multiple line message?
我尝试像这样在 GCC 编译器中使用 #error 指令:
#error "The charging pins aren't differing! One pin cannot be used for multiple purposes!"
This 说,我应该使用双引号,这样参数将是一个字符串常量,我可以在其中使用撇号。但是,我希望这个字符串以多行的形式出现在源代码中,例如:
#error "The charging pins aren't differing!
One pin cannot be used for multiple purposes!"
然后,我收到了一些错误信息:
warning: missing terminating " character
#error "The charging pins aren't differing! One pin
error: missing terminating " character
cannot be used for multiple purposes!"
如果我在第一行末尾插入一个黑斜杠,诊断消息将包含第二行开头和第一个单词 (One) 之间的所有空格 b。如果两行都是字符串,则诊断消息会显示内部双引号。
所以问题是:我怎样才能实现这个输出? (或类似的没有双引号,但包括撇号)
#error "The charging pins aren't differing! One pin cannot be used for multiple purposes!"
不幸的是,你不能拥有一切。
- 要么您必须去掉撇号,以便消息仅包含被视为有效的预处理标记。
- 或者您可以将其作为字符串写在一行中。
- 或者你可以写两个字符串文字并用
\
换行。您不能在字符串文字中间执行此操作,因为那样它就不是有效的预处理标记。这会使输出看起来很奇怪,例如:error: "hello" "world"
.
- 依靠两个字符串文字的预处理器连接是行不通的,因为错误指令只会查找,直到它在源代码中发现换行符。无论如何,错误指令都会将您键入的所有内容转换为字符串。
相关翻译阶段(来自 C17 5.1.1.2)按以下顺序执行:
2) Each instance of a backslash character () immediately followed by a new-line
character is deleted, splicing physical source lines to form logical source lines.
3) The source file is decomposed into preprocessing tokens and sequences of
white-space characters (including comments).
4) Preprocessing directives are executed, ...
6) Adjacent string literal tokens are concatenated.
#error
在第 4 步中执行,早于第 6 步中的字符串文字连接。
我个人认为最好的解决办法是跳过撇号:
#error The charging pins are not differing! \
One pin cannot be used for multiple purposes!
稍微修正一下英语,您就可以在可读的源代码和可读的错误消息之间获得最佳折衷。
如所述here
Neither ‘#error
’ nor ‘#warning
’ macro-expands its argument.
Internal whitespace sequences are each replaced with a single space. The line must consist of complete tokens. It is wisest to make the argument of these directives be a single string constant; this
avoids problems with apostrophes and the like.
所以你只能在单行中使用它。
#include <stdio.h>
//#define var 10
#ifdef var
#error "The charging pins aren't differing! One pin cannot be used for multiple purposes!"
#endif
int main(void){
printf("in main() \n");
return 0;
}
我尝试像这样在 GCC 编译器中使用 #error 指令:
#error "The charging pins aren't differing! One pin cannot be used for multiple purposes!"
This 说,我应该使用双引号,这样参数将是一个字符串常量,我可以在其中使用撇号。但是,我希望这个字符串以多行的形式出现在源代码中,例如:
#error "The charging pins aren't differing!
One pin cannot be used for multiple purposes!"
然后,我收到了一些错误信息:
warning: missing terminating " character
#error "The charging pins aren't differing! One pin
error: missing terminating " character
cannot be used for multiple purposes!"
如果我在第一行末尾插入一个黑斜杠,诊断消息将包含第二行开头和第一个单词 (One) 之间的所有空格 b。如果两行都是字符串,则诊断消息会显示内部双引号。
所以问题是:我怎样才能实现这个输出? (或类似的没有双引号,但包括撇号)
#error "The charging pins aren't differing! One pin cannot be used for multiple purposes!"
不幸的是,你不能拥有一切。
- 要么您必须去掉撇号,以便消息仅包含被视为有效的预处理标记。
- 或者您可以将其作为字符串写在一行中。
- 或者你可以写两个字符串文字并用
\
换行。您不能在字符串文字中间执行此操作,因为那样它就不是有效的预处理标记。这会使输出看起来很奇怪,例如:error: "hello" "world"
. - 依靠两个字符串文字的预处理器连接是行不通的,因为错误指令只会查找,直到它在源代码中发现换行符。无论如何,错误指令都会将您键入的所有内容转换为字符串。
相关翻译阶段(来自 C17 5.1.1.2)按以下顺序执行:
2) Each instance of a backslash character () immediately followed by a new-line character is deleted, splicing physical source lines to form logical source lines.
3) The source file is decomposed into preprocessing tokens and sequences of white-space characters (including comments).
4) Preprocessing directives are executed, ...
6) Adjacent string literal tokens are concatenated.
#error
在第 4 步中执行,早于第 6 步中的字符串文字连接。
我个人认为最好的解决办法是跳过撇号:
#error The charging pins are not differing! \
One pin cannot be used for multiple purposes!
稍微修正一下英语,您就可以在可读的源代码和可读的错误消息之间获得最佳折衷。
如所述here
Neither ‘#
error
’ nor ‘#warning
’ macro-expands its argument. Internal whitespace sequences are each replaced with a single space. The line must consist of complete tokens. It is wisest to make the argument of these directives be a single string constant; this avoids problems with apostrophes and the like.
所以你只能在单行中使用它。
#include <stdio.h>
//#define var 10
#ifdef var
#error "The charging pins aren't differing! One pin cannot be used for multiple purposes!"
#endif
int main(void){
printf("in main() \n");
return 0;
}