是否#IFDEF(或其他preprocessor指令)工作函数声明(要测试功能的存在)? [英] Does #ifdef (or other Preprocessor Directives) Work for Function Declarations (to Test for Function Existence)?

查看:213
本文介绍了是否#IFDEF(或其他preprocessor指令)工作函数声明(要测试功能的存在)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么不以下code按预期方式工作?

 无效foobar的(INT);的#ifndef FOOBAR
  的printf(FOOBAR存在);
#万一

它的总是的打印信息;它显然不能检测功能的存在作为一个实体。 (它是一个过载的问题?)

为什么不能够 #IFDEF (或其变体)检测函数声明?声明应可于pre-处理,所以它应该工作,不应该吗?如果没有,是否有替代或变通?


解决方案

  

声明应提供
  pre-处理,所以它应该工作,
  不应该吗?


在pre处理器汇编(因此pre)前工作,以便有在这一点上没有编译符号,只是文本和文本扩展。的$ P $对。处理器和编译器是完全独立的,并独立开展工作相互的,区别在于:$ P $对处理器修改传递给编译器的源的事实。

在做类似的pre处理器的典型模式是使用相同的功能的使用配对函数声明定义常数:

 的#define FOO#IFDEF FOO
 无效美孚(INT);
#万一#IFDEF FOO
   的printf(foo是否存在);
#万一

Why doesn’t the following code work as expected?

void foobar(int);

#ifndef foobar
  printf("foobar exists");
#endif

It always prints the message; it obviously cannot detect the existence of a function as an entity. (Is it an over-loading issue?)

Why can’t #ifdef (or its variants) detect function declarations? Declarations should be available at pre-processing, so it should work, shouldn’t it? If not, is there an alternative or work-around?

解决方案

Declarations should be available at pre-processing, so it should work, shouldn’t it?

The pre-processor operates before the compilation (hence the "pre") so there are no compiled symbols at that point, just text and text expansion. The pre-procesor and the compiler are distinctly separate and work independantly of each other, except for the fact that the pre-processor modifies the source that is passed to the compiler.

The typical pattern to doing something like with the pre-processor is to pair the function declaration with the function usage using the same define constant:

#define FOO

#ifdef FOO
 void foo(int);
#endif

#ifdef FOO
   printf("foo exists");
#endif

这篇关于是否#IFDEF(或其他preprocessor指令)工作函数声明(要测试功能的存在)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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