#ifdef __cplusplus  // C++编译环境中才会定义__cplusplus (plus就是"+"的意思)
extern "C" {      // 告诉编译器下面的函数是c语言函数(因为c++和c语言对函数的编译转换不一样,主要是c++中存在重载)
#endif

其实很简单,作用就是:如果此头文件是在c++中使用的(就是一个.cpp文件include此头文件,而不是一个.c文件),那么函数名称粉碎方式仍然使用C中的方式。(就是此函数编译后在object文件中的内部名称和C中是兼容的)。

比如:void foo1();
如果c文件包含了它,生成的object文件中此函数被命名为 c_foo1,而在cpp文件中包含了它,生成的object中此函数命名为cpp_foo1,使用extern "C"后,可以强制使其仍然为"c_foo1"。

这么做的目的是为了在c和cpp中互相调用函数命名一致,否则会出现链接错误。

看一些程序的时候老是有

#ifdef __cplusplus
extern "C" {
#endif

的定义,搞清楚是怎么回事:

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               

  当我们想从C++中调用C的库时,(注,驱动是用C写的,连new、delete也不能用,郁闷)不能仅仅说明 一个外部函数,因为调用C函数的编译代码和调用C++函数的编译代码是不同的。如果你仅说明一个外部函数, C++编译器假定它是

C++的函数编译成功了,但当你连接时会发现很可爱的错误。
  解决的方法就是指定它为C函数:extern "c" 函数描述 

  指定一群函数的话:

extern "C"{
//n个函数描述
} 

如果想C和C++混用的话:

#ifdef _cplusplus
extern "C"{
#endif 

//n个函数描述 

#ifdef _cplusplus
}
#endif

  extern "C" 表示编译生成的内部符号名使用C约定。

  C++支持函数重载,而C不支持,两者的编译规则也不一样。函数被C++编译后在符号库中的名字与C语言的不同。例如,假设某个函数的原型为: void foo( int x, int y ); 该函数被C编译器编译后在符号库中的名字可能为_foo,而C++编译器则会产生像_foo_int_int之类的名字(不同的编译器可能生成的名字不同,但是都采用了相同的机制,生成的新名字称为“mangled name”)。_foo_int_int这样的名字包含了函数名、函数参数数量及类型信息,C++就是靠这种机制来实现函数重载的。

  一、在C++中使用C的函数

//test.c
#include <stdio.h>
void mytest()
{
  printf("mytest in .c file ok\n");
}
//C++引用C函数的例子
//main.cpp
extern "C"
{
  void mytest();
}
int main()
{
  mytest();
  ;
}

  二、在C中使用C++的函数

  在C中引用C++语言中的函数和变量时,C++的函数或变量要声明在extern "C"{}里,但是在C语言中不能使用extern "C",否则编译出错。

//test.cpp
#include <stdio.h>
extern "C"
{
    void mytest()
    {
        printf("mytest in .cpp file ok\n");
    }
}
//在C中引用C++函数
//main.c
void mytest();
int main()
{
mytest();
;
}

  三、综合使用

  一般我们都将函数声明放在头文件,当我们的函数有可能被C或C++使用时,我们无法确定是否要将函数声明在 extern "C" 里,所以,我们应该添加

#ifdef __cplusplus
extern "C"
{
#endif

//函数声明

#ifdef __cplusplus
}
#endif

如果我们注意到,很多头文件都有这样的用法,比如string.h,等等。

//test.h
#ifdef __cplusplus
#include <iostream>
using namespace std;
extern "C"
{
#endif

void mytest();

#ifdef __cplusplus
}
#endif

这样,可以将mytest()的实现放在.c或者.cpp文件中,可以在.c或者.cpp文件中 include "test.h" 后,使用头文件里面的函数,而不会出现编译错误。

//test.c
#include "test.h"
void mytest()
{
    #ifdef __cplusplus
        cout << "cout mytest extern ok " << endl;
    #else
        printf("printf mytest extern ok n");
    #endif
}
//main.cpp
#include "test.h"
int main()
{
    mytest();
    ;
}

extern "C" 的用意

前些天,编程序是用到了很久以前写的C程序,想把里面的函数利用起来,连接发现出现了找不到具体函数的错误:

以下是假设旧的C程序库

C的头文件

/*-----------c.h--------------*/
#ifndef _C_H_
#define _C_H_

extern int add(int x, int y);

#endif

C的源文件

/*-----------c.c--------------*/
int add(int x, int y)
{
    return x+y;
}

C++的调用

/*-----------cpp.cpp--------------*/
#include "c.h"

void main()
{ 

    add(, );

}

  这样编译会产生错误cpp.obj : error LNK2001: unresolved external symbol "int __cdecl add(int,int)" (?add@@YAHHH@Z),原因是找不到add的目标模块这才令我想起C++重载的函数命名方式和C函数的命名方式。

  让我们回顾一下:C中函数编译后命名会在函数名前加以"_",比如add函数编译成obj文件时的实际命名为_add,而c++命名则不同,为了实现函数重载同样的函数名add因参数的不同会被编译成不同的名字

例如:

  int add(int , int)      ==>  add@@YAHHH@Z,

  float add(float , float ) ==>  add@@YAMMM@Z,

  以上是VC6的命名方式,不同的编译器会不同,总之不同的参数同样的函数名将编译成不同目标名,以便于函数重载是调用具体的函数。

  编译cpp.cpp中编译器在cpp文件中发现 add(1, 0); 的调用而函数声明为 extern int add(int x, int y); 编译器就决定去找 add@@YAHHH@Z,可惜他找不到,因为C的源文件把 extern int add(int x, int y); 编译成 _add 了;为了解决这个问题,C++采用了extern "C",这就是我们的主题。想要利用以前的C程序库,那么你就要学会它,我们可以看以下标准头文件你会发现,很多头文件都有以下的结构

#ifndef __H
#define __H

#ifdef __cplusplus
extern "C"
{
#endif

extern int f1(int, int);
extern int f2(int, int);
extern int f3(int, int);

#ifdef __cplusplus
}
#endif

#endif 
/*__H*/如果我们仿制该头文件可以得到

#ifndef _C_H_
#define _C_H_

#ifdef __cplusplus
extern "C"
{
#endif

extern int add(int, int);

#ifdef __cplusplus
}
#endif

#endif 
/* _C_H_ */ 这样编译

/*-----------c.c--------------*/
int add(int x, int y){
    return x+y;
}

这时源文件为*.c,__cplusplus没有被定义,extern "C" {}这时没有生效,对于C他看到只是extern int add(int, int); add函数编译成_add(int, int);

而编译c++源文件

/*-----------cpp.cpp--------------*/
#include "c.h"
void main()
{
    add(, );
}

这时源文件为*.cpp,__cplusplus被定义,对于C++他看到的是 extern "C" {extern int add(int, int);} 编译器就会知道 add(1, 0); 调用的C风格的函数,就会知道去c.obj中找_add(int, int)而不是add@@YAHHH@Z;这也就为什么DLL中常看见extern "C" {},windows是采用C语言编制他首先要考虑到C可以正确调用这些DLL,而用户可能会使用C++而extern "C" {}就会发生作用。

 
 

#ifdef __cplusplus extern c #endif 的作用的更多相关文章

  1. “#ifdef __cplusplus extern "C" { #endif”的定义

    平时我们在linux c平台开发的时候,引用了一些Cpp或者C的代码库,发现一些头文件有如下代码条件编译. #ifdef __cplusplus extern "C" { #end ...

  2. “#ifdef __cplusplus extern "C" { #endif”的定义-----C和C++的互相调用

    "#ifdef __cplusplus extern "C" { #endif"的定义 看一些程序的时候老是有 "#ifdef __cplusplus ...

  3. #ifdef __cplusplus extern "C" { #endif”的定义

      平时我们在linux c平台开发的时候,引用了一些Cpp或者C的代码库,发现一些头文件有如下代码条件编译. #ifdef __cplusplus extern "C" { #e ...

  4. #ifdef __cplusplus extern "C" { #endif”的定义的含义

    看一些程序的时候老是有“#ifdef __cplusplusextern "C" {#endif”的定义,搞搞清楚是怎么回事: Microsoft-Specific Predefi ...

  5. C++ 为什么要使用#ifdef __cplusplus extern "C" { #endif

    转载:http://www.cnblogs.com/ayanmw/archive/2012/03/15/2398593.html 转载:http://blog.csdn.net/zkl99999/ar ...

  6. 备忘录:“#ifdef __cplusplus extern "C" { #endif”的定义

    看一些程序的时候老是有“#ifdef __cplusplusextern "C" {#endif”的定义,搞搞清楚是怎么回事: Microsoft-Specific Predefi ...

  7. #ifdef __cplusplus extern "C" { #endif

    1.在好多程序中我们会遇到下面代码段 #ifdef __cplusplus        extern "C" {        #endif //c语法代码段 #ifdef __ ...

  8. #ifdef __cplusplus extern "C" { #endif 的解释

    好多程序中都会遇到下列代码段: #ifdef __cplusplus extern "C" { #endif /****************** C语法代码段 ******** ...

  9. undefined reference to `recvIpcMsg(int, ipc_msg*)'——#ifdef __cplusplus extern "C" { #endif

    最近在弄一个进程间通信,原始测试demon用c语言写的,经过测试ok,然后把接口封装起来了一个send,一个recv. 使用的时候send端是在一个c语言写的http服务端使用,编译ok没有报错,但是 ...

随机推荐

  1. iOS中Git的使用

    打开终端: 查看Git的版本的终端命令:git —version 输入:ssh 查看是否已经存在ssh. 如果存在,先将已有的ssh备份,或者将新建的ssh生成到另外的目录下 如果不存在,通过默认的参 ...

  2. opencv多平台环境搭建及使用

    windows平台: 一.安装opencv 下载地址:http://opencv.org/ 依据平台下载相应源码包 安装流程就是一个解压过程.不再赘述. 解压完,效果图: 源码树结构参看http:// ...

  3. Oracle REGEXP_INSTR 用法

    原文出处 ORACLE中的支持正则表达式的函数主要有下面四个:    1,REGEXP_LIKE :与LIKE的功能相似    2,REGEXP_INSTR :与INSTR的功能相似    3,REG ...

  4. 19个非常有用的Javascript类库

    Blackbird是一款非常酷的JavaScript调试工具,带有一个漂亮的界面显示和过滤调试信息. http://www.gscottolson.com/blackbirdjs/ Treesaver ...

  5. WPF Radio组的绑定

    都是控件编,RadioButtion 简单绑定使用,model.cs下边定义属性 private int _isSuccess; public int IsSuccess { get { return ...

  6. c语言学习之基础知识点介绍(七):循环结构

    本节主要介绍循环结构 一.while循环 /* 语法: while(表达式){ //循环体; } 注意:循环变量.循环条件和循环控制语句三者缺一不可. 例如: */ ; //循环变量 ){ //循环条 ...

  7. C#中volatile的用法

    恐怕比较一下volatile和synchronized的不同是最容易解释清楚的.volatile是变量修饰符,而synchronized则作用于一段代码或方法:看如下三句get代码: int i1;  ...

  8. $(obj).data() 绑定和获取数据的应用

    1.解说 data() 方法向被选元素附加数据,或者从被选元素获取数据. 例如:$("#id").data("name","xiao"); ...

  9. c# ADO连接Access 执行Open后程序自动退出

    今天利用ADO连接Access数据库的时候遇到了前所未见的问题,Access数据库连接串,OleDbConnection,open的时候,系统就会自动关闭所有调试. 我就很纠结了,这个AccessHe ...

  10. C#程序将对象保存为json文件的方法

    首先,从NuGet上下载JSON .Net,安装到所需项目中. 对象obj保存到文件的步骤: 1. 创建文件 // 获取当前程序所在路径,并将要创建的文件命名为info.json string fp ...