`
umh495qm
  • 浏览: 9847 次
最近访客 更多访客>>
社区版块
存档分类
最新评论

#ifdef __cplusplus是什么意思

 
阅读更多

#ifdef __cplusplus是什么意思
2009年10月28日
  

#ifdef __cplusplus是什么意思?
  Microsoft-Specific Predefined Macros
  __cplusplus Defined for C++ programs only.
  意思是说,如果是C++程序,就使用
  extern "C"{
  而这个东东,是指在下面的函数不使用的C++的名字修饰,而是用C的
  The following code shows a header file which can be used by C and C++ client applications:
  // MyCFuncs.h
  #ifdef __cplusplus
  extern "C" { // only need to export C interface if
  // used by C++ source code
  #endif
  __declspec( dllimport ) void MyCFunc();
  __declspec( dllimport ) void AnotherCFunc();
  #ifdef __cplusplus
  }
  #endif
  #ifdef __cplusplus 倒底是什么意思?
  时常在cpp的代码之中看到这样的代码:
  #ifdef __cplusplus
  extern "C" {
  #endif
  //一段代码
  #ifdef __cplusplus
  }
  #endif
    这样的代码到底是什么意思呢?首先,__cplusplus是cpp中的自定义宏,那么定义了这个宏的话表
  示这是一段cpp的代码,也就是说,上面的代码的含义是:如果这是一段cpp的代码,那么加入extern
  "C"{和}处理其中的代码。
    要明白为何使用extern "C",还得从cpp中对函数的重载处理开始说起。在c++中,为了支持重载机
  制,在编译生成的汇编码中,要对函数的名字进行一些处理,加入比如函数的返回类型等等.而在C中,
  只是简单的函数名字而已,不会加入其他的信息.也就是说:C++和C对产生的函数名字的处理是不一样的.
    比如下面的一段简单的函数,我们看看加入和不加入extern "C"产生的汇编代码都有哪些变化:
  int f(void)
  {
  return 1;
  }
    在加入extern "C"的时候产生的汇编代码是:
  .file "test.cxx"
  .text
  .align 2
  .globl _f
  .def _f; .scl 2; .type 32; .endef
  _f:
  pushl %ebp
  movl %esp, %ebp
  movl $1, %eax
  popl %ebp
  ret
    但是不加入了extern "C"之后
  .file "test.cxx"
  .text
  .align 2
  .globl __Z1fv
  .def __Z1fv; .scl 2; .type 32; .endef
  __Z1fv:
  pushl %ebp
  movl %esp, %ebp
  movl $1, %eax
  popl %ebp
  ret
    两段汇编代码同样都是使用gcc -S命令产生的,所有的地方都是一样的,唯独是产生的函数名,一
  个是_f,一个是__Z1fv。
    明白了加入与不加入extern "C"之后对函数名称产生的影响,我们继续我们的讨论:为什么需要使用
  extern "C"呢?C++之父在设计C++之时,考虑到当时已经存在了大量的C代码,为了支持原来的C代码和
  已经写好C库,需要在C++中尽可能的支持C,而 extern "C"就是其中的一个策略。
    试想这样的情况:一个库文件已经用C写好了而且运行得很良好,这个时候我们需要使用这个库文件
  ,但是我们需要使用C++来写这个新的代码。如果这个代码使用的是C++的方式链接这个C库文件的话,那
  么就会出现链接错误.我们来看一段代码:首先,我们使用C的处理方式来写一个函数,也就是说假设这个
  函数当时是用C写成的:
  //f1.c
  extern "C"
  {
  void f1()
  {
  return;
  }
  }
    编译命令是:gcc -c f1.c -o f1.o 产生了一个叫f1.o的库文件。再写一段代码调用这个f1函数:
  // test.cxx
  //这个extern表示f1函数在别的地方定义,这样可以通过
  //编译,但是链接的时候还是需要
  //链接上原来的库文件.
  extern void f1();
  int main()
  {
  f1();
  return 0;
  }
    通过gcc -c test.cxx -o test.o 产生一个叫test.o的文件。然后,我们使用gcc test.o f1.o来链
  接两个文件,可是出错了,错误的提示是:
  test.o(.text + 0x1f):test.cxx: undefine reference to 'f1()'
    也就是说,在编译test.cxx的时候编译器是使用C++的方式来处理f1()函数的,但是实际上链接的库
  文件却是用C的方式来处理函数的,所以就会出现链接过不去的错误:因为链接器找不到函数。
    因此,为了在C++代码中调用用C写成的库文件,就需要用extern "C"来告诉编译器:这是一个用C写
  成的库文件,请用C的方式来链接它们。
    比如,现在我们有了一个C库文件,它的头文件是f.h,产生的lib文件是f.lib,那么我们如果要在
  C++中使用这个库文件,我们需要这样写:
  extern "C"
  {
  #include "f.h"
  }
    回到上面的问题,如果要改正链接错误,我们需要这样子改写test.cxx:
  extern "C"
  {
  extern void f1();
  }
  int main()
  {
  f1();
  return 0;
  }
    重新编译并且链接就可以过去了.
    总结
    C和C++对函数的处理方式是不同的.extern "C"是使C++能够调用C写作的库文件的一个手段,如果要
  对编译器提示使用C的方式来处理函数的话,那么就要使用extern "C"来说明。
分享到:
评论

相关推荐

    解译#ifdef __cplusplus

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

    #ifdef __cplusplus深度剖析

    #ifdef __cplusplus深度剖析

    错误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++

    #ifndef __cplusplus #error "eh.h is only for C++!" #endif /* Define _CRTIMP */ #ifndef _CRTIMP #ifdef _DLL #define _CRTIMP __declspec(dllimport) #else /* ndef _DLL */ #define _CRTIMP #endif /* _DLL...

    dx8sdk+lib+include

    #ifdef __cplusplus #define D3DXINLINE inline #else #define D3DXINLINE _inline #endif #endif #define D3DX_DEFAULT ULONG_MAX #define D3DX_DEFAULT_FLOAT FLT_MAX #include "d3dx8math.h" #include "d3dx8...

    基于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 *

    C语言编写的俄罗斯方块游戏源码

    #ifdef __cplusplus #define __CPPARGS ... #else #define __CPPARGS #endif #define MINBOXSIZE 15 /* 最小方块的尺寸 */ #define BGCOLOR 7 /* 背景着色 */ #define GX 200 #define GY 10 #define SJNUM ...

    视频监控系统源代码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...

    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;);...

    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

    snmp开发环境

    所以只要把下在的内容加到winspool.h文件末尾#ifdef __cplusplus的前面就可以了: Cpp代码 1. BOOL 2. WINAPI 3. SetDefaultPrinterA( 4. LPCTSTR pszPrinter 5. ); 6. 7. BOOL 8. WINAPI 9. ...

    clockMan1.h

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

    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语言代码中调用C++代码的方法示例

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

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

    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

    内存加载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 ...

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

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

Global site tag (gtag.js) - Google Analytics