C#error 宏可以显示多行消息吗?

标签 c string c-preprocessor directive

我尝试像这样在 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!

稍微修正一下英语,您就可以在可读的源代码和可读的错误消息之间获得最佳折衷。

关于C#error 宏可以显示多行消息吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52386377/

相关文章:

c++ - 在 C 中使用局部变量创建类似于函数的 C++ 模板

c - OpenGL-GLSL 绘制 3D GL_LINES 顶点着色器与顶点

c - 什么时候以及为什么我必须关闭管道?

java - com.fasterxml.jackson.core.JsonParseException : Unexpected character ('\' (code 92)) [Java]

c++ - Visual Studio C++ 预处理器定义失败,路径从 "u"开始

将在该 block 之后执行一段代码和特定命令的 C++ MACRO

c - 将操作数放在 getopt() 的前面

c++ - OS X Lion 上的 libplist 编译错误 : string. h not found

python - 我如何从字符串中获取特定单词的下两个单词?

java - 当字符串变量的输入前面有整数输入时,该输入将被跳过