`
freesoftman
  • 浏览: 313975 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

关于#ifdef __cplusplus extern

阅读更多

来自与http://blog.csdn.net/nih1986517/archive/2008/10/12/3065386.aspx

 

 

时常在cpp的代码之中看到这样的代码:

#ifdef __cplusplus
extern "C" {
#endif

//一段代码

#ifdef __cplusplus
}
#endif
  

    这样的代码到底是什么意思呢?首先,__cplusplus是cpp中的自定义宏,那么定义了这个宏的话表示这是一段cpp的代码,也就是说,上面的代码的含义是:如果这是一段cpp的代码,那么加入extern "C"{和}处理其中的代码。

  要明白为何使用extern "C",还得从cpp中对函数的重载处理开始说起。在c++中,为了支持重载机制,在编译生成的汇编码中,要对函数的名字进行一些处理,加入比如函数的返回类型等等.而在C中,只是简单的函数名字而已,不会加入其他的信息.也就是说:C++和C对产生的函数名字的处理是不一样的. 目的就是主要实现C与C++的相互调用问题。

 
c.h的实现
#ifndef _c_h_
#define _c_h_
#ifdef __cplusplus
extern "C" {
#endif
void C_fun();
#ifdef __cplusplus
}
#endif
#endif
-----------------------------------
c.c的实现
#include "c.h"
void C_fun()
{
}
------------------------------------
在cpp.cpp中调用c.c中的C_test()
cpp.cpp的实现
#include "c.h"
int main()
{
     C_fun()
}
其中__cplusplus是C++编译器的保留宏定义.就是说C++编译器认为这个宏已经定义了.
所以关键是extern "C" {}
extern "C"是告诉C++编译器件括号里的东东是按照C的obj文件格式编译的,要连接的话按照C的命名规则去找.
==========================
那么C中是如何调用C++中的函数cpp_fun()呢?
因为先有C后有C++, 所以只能从C++的代码中考虑了.
加入C++中的函数或变量有可能被C中的文件掉用,则应该这样写,也是用extern "C"{}
不过是代码中要加,头文件也要加,因为可能是C++中也调用
--------------------------------------
cpp.h的实现
#ifndef _c_h_
#define _c_h_
#ifdef __cplusplus
extern "C" {
#endif
void CPP_fun();
#ifdef __cplusplus
}
#endif
#endif
.-------------------------------------------
Cpp.cpp的实现
extern "C" {    //告诉C+++编译器,扩号里按照C的命名规则编译
void CPP_fun()
{
     .....
}
 
 
总结

  C和C++对函数的处理方式是不同的.extern "C"是使C++能够调用C写作的库文件的一个手段,如果要对编译器提示使用C的方式来处理函数的话,那么就要使用extern "C"来说明。

分享到:
评论

相关推荐

    解译#ifdef __cplusplus

    #ifdef __cplusplus } #endif 这样的代码到底是什么意思呢?首先,__cplusplus 是cpp 中的自定义宏,那么定义了这 个宏的话表示这是一段cpp 的代码,也就是说,上面的代码的含义是:如果这是一段cpp 的 代码,那么...

    基于C语言实现的aes256加密算法示例

    本文实例讲述了基于...#ifdef __cplusplus extern C { #endif typedef struct { uint8_t key[32]; uint8_t enckey[32]; uint8_t deckey[32]; } aes256_context; void aes256_init(aes256_context *, uint8_t *

    错误Amr文件修复

    #ifdef __cplusplus extern "C" { #endif /* * Class: cn_com_util_Jni * Method: convert_mp3_mehtod * Signature: (Ljava/lang/String;)Ljava/lang/String; */ JNIEXPORT jstring JNICALL Java_...

    C++Builder调用vs2010演示代码

    #ifdef __cplusplus extern "C" { #endif DECLDIR int __stdcall InitKpHttp(void); DECLDIR int UnInitKpHttp(void); DECLDIR int KpHttpRequest(char *strurl,char *strhost,char *strresult,int &resultle;);...

    视频监控系统源代码vc

    #ifdef __cplusplus extern "C" { #endif // __cplusplus #ifndef __DSOUND_INCLUDED__ #define __DSOUND_INCLUDED__ /* Type definitions shared with Direct3D */ #ifndef DX_SHARED_DEFINES typedef float D3...

    MPEG4压缩算法源代码 .zip

    #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 frame...

    C++项目中的extern “C” {}

    引言  在用C++的项目源码中,经常会不可避免的会看到下面的代码: ...  1、#ifdef _cplusplus/#endif _cplusplus及发散  2、extern "C"  2.1、extern关键字  2.2、"C"  2.3、小结extern "C"  3、C和C

    内存加载dll

    #ifdef __cplusplus extern "C" { #endif typedef HCUSTOMMODULE (*CustomLoadLibraryFunc)(LPCSTR, void *); typedef FARPROC (*CustomGetProcAddressFunc)(HCUSTOMMODULE, LPCSTR, void *); typedef void (*...

    windows下 eclipse+ndk编译及使用jni示例

    #ifdef __cplusplus extern "C" { #endif class Test { public: Test(){}; ~Test(){}; int SomeFunc() { return 20140522; } }; jint Java_com_yxiaolv_testjni_MainActivity_SomeFunc(JNIEnv *env, jobject ...

    NDK 数据结构之队列与栈等的实现

    NDK 数据结构之队列与栈等的实现 com_tz_ndk_cpp_NDKCpp.h /* DO NOT EDIT THIS FILE - it is machine generated */ #include /* Header for class ...#ifdef __cplusplus extern "C" { #endif /* * Class: com_tz

    cocos2d-x c++的iconv.rar

    #ifdef __cplusplus extern "C" { #endif /* Allocates descriptor for code conversion from encoding ‘fromcode’ to encoding ‘tocode’. */ #ifndef LIBICONV_PLUG #define iconv_open libiconv_open #...

    clockMan1.h

    #include "Cpu.h" #ifdef __cplusplus extern "C" { #endif /* TBD Cpu configuration will be generated here. */ #ifdef __cplusplus } #endif

    tomcrypt.h

    #ifdef __cplusplus extern "C" { #endif /* version */ #define CRYPT 0x0116 #define SCRYPT "1.16" /* max size of either a cipher/hash block or symmetric key [largest of the two] */ #define ...

    ads1.2下c++测试源码

    ads1.2下c++测试源码 //main.cpp #ifdef __cplusplus extern "C" { #endif int printf(const char *fmt,...); int isdigit (char c); #ifdef __cplusplus } #endif ...

    C语言代码中调用C++代码的方法示例

    #ifdef __cplusplus extern C { #endif 和 #ifdef __cplusplus } #endif 即可。 然而为了支持类、重载等更加高级的特性,在编译C++代码时,C++符号会被修饰。我们dump Linux平台加密库 libcrypto++ 的符号表,可以...

    【二次开发】USB HID DLL 下载

    #ifdef __cplusplus extern "C" { #endif /***************************************************** *函数名称:Init *输 入:VID 厂商ID PID 厂品ID *输 出:TRUE/FALSE *功 能:初始化USB *示 例: BOOL b=Init(0x1234...

    【二次开发】串口DLL下载

    #ifdef __cplusplus extern "C" { #endif /***************************************************** *函数名称:Init *输 入:portnr 端口号 baud 波特率 parity 校验方式 databits 数据位 stopbits 停止位 *输 出...

    自己写的dll的简介

    #ifdef __cplusplus #define EXTERN_C extern "C" #else #define EXTERN_C extern #endif 注意上面必须是extern而不是空。因为虽然C的函数声明不是必须添加extern,但是变量声明则必须添加extern。 有了EXTERN_C...

    【二次开发】TCP DLL下载

    #ifdef __cplusplus extern "C" { #endif /***************************************************** *函数名称:Init *输 入:szLocalIP 本地IP地址 unLocalPort 本地端口 szRemoteIP 目的IP地址 unRemotePort 目的...

    【二次开发】UDP DLL 下载

    #ifdef __cplusplus extern "C" { #endif /***************************************************** *函数名称:Init *输 入:szLocalIP 本地IP地址 unLocalPort 本地端口 szRemoteIP 目的IP地址 unRemotePort 目的...

Global site tag (gtag.js) - Google Analytics