有没有办法让 C 预处理器解析 #error 语句中的宏?
正如标题所说。我想在 #error 语句的文本中使用预处理器宏:
#define SOME_MACRO 1
#if SOME_MACRO != 0
#error "SOME_MACRO was not 0; it was [value of SOME_MACRO]"
#endif
在此示例中,我希望预处理器将 [value of SOME_MACRO] 解析为 SOME_MACRO
的实际值在本例中为 1。这应该在预处理器、编译器或任何进程 #error 打印错误输出之前发生
有没有办法做到这一点,或者这是不可能的?
我不想知道是否有 ISO C++ 标准方法可以做到这一点,因为据我所知,任何 ISO C++ 标准中都没有声明预处理器指令 #error
。不过,我知道 GCC 和 Visual C++ 支持 #error
。但我的问题并不是特定于这些编译器,我只是好奇是否有任何 C/C++ 编译器/预处理器可以做到这一点。
我尝试搜索该主题,但没有成功。
Just as the title says. I want to use a preprocessor macro in the text of an #error statement:
#define SOME_MACRO 1
#if SOME_MACRO != 0
#error "SOME_MACRO was not 0; it was [value of SOME_MACRO]"
#endif
In this example I want the preprocessor to resolve [value of SOME_MACRO]
to the actual value of SOME_MACRO
which in this case is 1. This should happen before the preprocessor, compiler or whatever processes #error prints the error output
Is there a way to do that or is this just not possible?
I don't want to know if there is an ISO C++ standard way to do that, because afaik the preprocessor directive #error
is not stated in any ISO C++ standard. However, I know GCC and Visual C++ support #error
. But my question is not specific to those compilers, I'm just curious if any C/C++ compiler/preprocessor can do that.
I tried to search for that topic but without any luck.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为了完整性,我建议的 C++0x 方式(使用与 Kirill 相同的技巧):
给出:
For completeness the C++0x way I suggested (using the same trick as Kirill):
Gives:
在 Visual Studio 中,您可以使用
pragma
message< /code>
如下:
这将生成两条消息,但您将获得
SOME_MACRO
的值。在 G++ 中,请使用以下代码(来自注释:g++ 版本 4.3.4 可以很好地使用括号,如上面的代码所示):In Visual Studio you can use
pragma
message
as follows:This will generate two messages, but you'll get the value of
SOME_MACRO
. In G++ use the following instead (from comments: g++ version 4.3.4 works well with parenthesis as in the code above):在 Visual Studio 2005 中生成“无法打开包含文件:'invalid_macro_value_1':没有这样的文件或目录”,并且在其他编译器上可能会生成类似的消息。
这并没有直接回答您有关使用 #error 的问题,但结果是相似的。
generates "Cannot open include file: 'invalid_macro_value_1': No such file or directory" in Visual Studio 2005 and probably similar messages on other compilers.
This doesn't answer your question directly about using #error, but the result is similar.