C/C 中的 #error 如何处理?工作?
我从 # 猜测它只是一个编译时实用程序。如何在C/C++程序中使用它?
在互联网上没有找到太多相关信息。任何链接都会有帮助。
I am guessing from # that it is only a compile-time utility. How can it be used in C/C++ programs?
Did not find much about it on the internet. Any links would be helpful.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
它导致编译器(或预处理器)输出错误消息。在 C++ 中,它还会导致翻译单元格式错误(即导致编译失败)。
如果您有多个可以定义的宏,并且希望确保只定义它们的某些组合,则可以使用#error 在定义了无效组合时导致编译失败。
如果您想确保某些代码块永远不会被编译(无论出于何种原因),它也很有用。
It causes the compiler (or preprocessor) to output the error message. In C++, it also renders the translation unit ill-formed (i.e., it causes compilation to fail).
If you have several macros that could be defined and you want to be sure that only certain combinations of them are defined, you can use
#error
to cause compilation to fail if an invalid combination is defined.It can also be useful if you want to be sure that some block of code is never compiled (for whatever reason).
对于检查编译器设置以及验证宏值组合很有用。一些随机的例子:
Useful to check compiler settings as well as verifying macro value combinations. Some random examples:
以下是解释
#error
和#warning
指令的 Gnu 预处理器文档的链接:http://gcc.gnu.org/onlinedocs/cpp/Diagnostics.html特别是:
另请参阅此问题了解这些指令的可移植性。
Here is a link to the documentation of the Gnu preprocessor explaining the
#error
and#warning
directives: http://gcc.gnu.org/onlinedocs/cpp/Diagnostics.htmlIn particular:
See also this question about the portability of these directives.