检查函数是否用 C 预处理器声明? [英] Check whether function is declared with C preprocessor?

查看:16
本文介绍了检查函数是否用 C 预处理器声明?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以告诉 C 预处理器检查是否声明了函数(不是宏)?我尝试了以下方法,但它似乎不起作用:

Is it possible to tell the C preprocessor to check whether a function (not a macro) is declared? I tried the following, but it doesn't appear to work:

#include <stdio.h>

int main(void)
{
#if defined(printf)
    printf("You support printf!
");
#else
    puts("Either you don't support printf, or this test doesn't work.");
#endif
    return 0;
}

推荐答案

没有.预处理器在 C 编译器和 C 编译器处理函数声明之前运行.预处理器仅用于文本处理.

No. Preprocessor runs before the C compiler and the C compiler processes function declarations. The preprocessor is only there for text processing.

但是,大多数头文件都包含诸如 _STDIO_H_ 之类的保护宏,您可以在预处理器阶段对其进行测试.但是,该解决方案不可移植,因为包含保护宏名称未标准化.

However, most header files have include guard macros like _STDIO_H_ that you can test for in the preprocessor stage. However, that solution is not portable as the include guard macro names are not standardized.

这篇关于检查函数是否用 C 预处理器声明?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆