C/C 中的 #error 如何处理?工作?

发布于 2024-09-01 08:27:51 字数 77 浏览 4 评论 0原文

我从 # 猜测它只是一个编译时实用程序。如何在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

谎言 2024-09-08 08:27:51

它导致编译器(或预处理器)输出错误消息。在 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).

谈场末日恋爱 2024-09-08 08:27:51

对于检查编译器设置以及验证宏值组合很有用。一些随机的例子:

#if !defined(_DLL)
#  error This code will only work properly when compiled with /MD
#endif

#if _WIN32_WINNT < 0x502
#  error Sorry, Windows versions prior to XP SP2 are not supported
#endif

#if defined(_APPLE) && defined(_LINUX)
#  error Conflicting operating system option selected, choose one.
#endif

Useful to check compiler settings as well as verifying macro value combinations. Some random examples:

#if !defined(_DLL)
#  error This code will only work properly when compiled with /MD
#endif

#if _WIN32_WINNT < 0x502
#  error Sorry, Windows versions prior to XP SP2 are not supported
#endif

#if defined(_APPLE) && defined(_LINUX)
#  error Conflicting operating system option selected, choose one.
#endif
╄→承喏 2024-09-08 08:27:51

以下是解释 #error#warning 指令的 Gnu 预处理器文档的链接:http://gcc.gnu.org/onlinedocs/cpp/Diagnostics.html

特别是:

指令#error导致
预处理器报告致命错误。
构成其余部分的代币
#error 后面的行用作
错误消息。

另请参阅此问题了解这些指令的可移植性。

Here is a link to the documentation of the Gnu preprocessor explaining the #error and #warning directives: http://gcc.gnu.org/onlinedocs/cpp/Diagnostics.html

In particular:

The directive #error causes the
preprocessor to report a fatal error.
The tokens forming the rest of the
line following #error are used as
the error message.

See also this question about the portability of these directives.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文