【问题标题】:Printing something with C preprocessor [duplicate]用C预处理器打印一些东西[重复]
【发布时间】:2012-06-09 18:34:42
【问题描述】:

可能重复:
Is there a portable way to print a message from the C preprocessor?

在大型代码库中导航时,有时很难猜测定义了什么,没有定义什么。因此,我想在编译时打印一些东西。例如:

#ifdef SOME_DEFINE
// I want a preprocessor to print something here so that 
//  I can know whether SOME_DEFINE is defined or not
#endif

这可能与 C 相关吗?
我在某处看到了一个叫#error 的东西。也许这是我唯一的选择,或者不是?

【问题讨论】:

标签: c c-preprocessor


【解决方案1】:

对于 MS Visual C++:

#define __PRINT(str) __pragma(message(str))

【讨论】:

    【解决方案2】:

    #error 指令的伟大之处在于,即使它不受支持,它也能正常工作!

    如果支持,编译器会告诉你"error: #error",如果不支持,编译器会告诉你"invalid preprocessing directive"。无论哪种方式,这都是一个致命错误,编译停止,编译器会告诉你哪一行是有问题的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-09
      • 1970-01-01
      • 1970-01-01
      • 2018-03-26
      • 2015-10-14
      相关资源
      最近更新 更多