#ifdef __cplusplus extern "C" { #endif //一段代码 #ifdef __cplusplus } #endif

经常看到别人的头文件 有这样的代码

#ifdef __cplusplus

extern "C" {

#endif

// C 样式的代码声明 

#ifdef __cplusplus

}

#endif

这样的代码到底是什么意思呢?

首先,__cpluspluscpp中的自定义宏,那么定义了这个宏的话表示这是一段cpp的代码,也就是说,上面的代码的含义是:如果这是一段cpp的代码,那么加入"extern "C"{" " }"处理其中的代码,其中{ }内部的代码是通过extern"C"进行处理。要明白为何使用extern"C",还得从cpp中对函数的重载处理开始说起。在c++中,为了支持重载机制(即支持同名函数)在编译生成的汇编码中,要对函数的名字进行一些处理,加入比如函数的返回类型等等.而在C中,只是简单的函数名字而已,不会加入其他的信息.也就是说:C++C对产生的函数名字的处理是不一样的.

如果是C语言编译的中间文件,要C++来调用,那么就需要这个了,C++有了extern "C"就会按照C语言的方法进行函数命名。这样编译出来的中间文件就是C样式的函数名,C、C++都可以调用。

如果 C++ 编译的中间文件,要C语言来调用,是不行的。

C++之父在设计C++之时,考虑到当时已经存在了大量的C代码,为了支持原来的C代码和已经写好C库,需要在C++中尽可能的支持C,而extern"C"就是其中的一个策略。因此,为了在C++代码中调用用C写成的库文件,就需要用extern"C"来告诉编译器:这是一个用C写成的库文件,请用C的方式来链接它们。

这个{}里面的#endif对应上面的#ifdef-cplusplus#ifdef-cplusplus对应最后的#endif, #ifdef#endif总是一一对应的,表明条件编译开始和结束。

 

一般考虑跨平台使用方法如下:

#if defined(__cplusplus)||defined(c_plusplus) //跨平台定义方法

extern "C"{

#endif

//... 正常的声明段

#if defined(__cplusplus)||defined(c_plusplus)

}

#endif

 

简单的用在windows下可以如下定义:

#ifdef __cplusplus

extern "C"{

//... 正常的声明段

}

#endif

  • 3
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
mpeg4编码库源代码,C++完整源代码,有需要饿同学尽管拿去。 // This is the header file describing // the entrance function of the encoder core // or the encore ... #ifdef __cplusplus extern "C" { #endif typedef struct _ENC_PARAM_ { int x_dim; // the x dimension of the frames to be encoded int y_dim; // the y dimension of the frames to be encoded float framerate;// the frame rate of the sequence to be encoded long bitrate; // the bitrate of the target encoded stream long rc_period; // the intended rate control averaging period long rc_reaction_period; // the reation period for rate control long rc_reaction_ratio; // the ratio for down/up rate control long max_key_interval; // the maximum interval between key frames int max_quantizer; // the upper limit of the quantizer int min_quantizer; // the lower limit of the quantizer int search_range; // the forward search range for motion estimation } ENC_PARAM; typedef struct _ENC_FRAME_ { void *image; // the image frame to be encoded void *bitstream;// the buffer for encoded bitstream long length; // the length of the encoded bitstream } ENC_FRAME; typedef struct _ENC_RESULT_ { int isKeyFrame; // the current frame is encoded as a key frame } ENC_RESULT; // the prototype of the encore() - main encode engine entrance int encore( unsigned long handle, // handle - the handle of the calling entity, must be unique unsigned long enc_opt, // enc_opt - the option for encoding, see below void *param1, // param1 - the parameter 1 (its actually meaning depends on enc_opt void *param2); // param2 - the parameter 2 (its actually meaning depends on enc_opt // encore options (the enc_opt parameter of encore()) #define ENC_OPT_WRITE 1024 // write the reconstruct image to files (for debuging) #define ENC_OPT_INIT 32768 // initialize the encoder for an handle #define ENC_OPT_RELEASE 65536 // release all the resource associated with the handle // return code of encore() #define ENC_OK 0 #define ENC_MEMORY 1 #define ENC_BA

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值