站起来孙燕姿现场:关于C和C++混合编程

来源:百度文库 编辑:偶看新闻 时间:2024/04/29 15:55:02

关于C和C++混合编程

2011-07-27 19:10 25人阅读 评论(0) 收藏 举报  

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的函数,或者在C中使用C++的函数。

 一://C++引用C函数的例子(C++调用C,extern "C" 的作用是:让C++连接器找调用函数的符号时采用C的方式 如)

//test.c

#include

void mytest()

{

 printf("mytest in .c file ok\n");

}

//main.cpp

extern "C"

{

void mytest();

}

int main()

{

 mytest();

 return 0;

}

上述也可以加个头文件

//test.h

void mytest()

在后在main.cpp中extern "C"

{

#include “test.h”

}

 

二://在C中引用C++函数(C调用C++,使用extern "C"则是告诉编译器把cpp文件中extern "C"定义的函数依照C的方式来编译封装接口,当然接口函数里面的C++语法还是按C++方式编译)

在C中引用C++语言中的函数和变量时,C++的函数或变量要声明在extern "C"{}里,但是在C语言中不能使用extern "C",否则编译出错。(出现错误: error C2059: syntax error : 'string',这个错误在网上找了很久,国内网站没有搜到直接说明原因的,原因是extern "C"是C++中的关键词,不是C的,所有会出错。那怎么用?看本文,或者搜extern "C"!)

//test.cpp

#include

extern "C"

{

void mytest()

{

 printf("mytest in .cpp file ok\n");

}

}

//main.c

void mytest();

int main()

{

 mytest();

 return 0;

}

三.//综合使用

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

#ifdef __cplusplus

 extern "C"

 {

 #endif

//函数声明

#ifdef __cplusplus

 }

 #endif

这样的话这个文件无论是被C或C++调用都可以,不会出现上面的那个错误:error C2059: syntax error : 'string'。

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

//test.h

#ifdef __cplusplus

#include

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

 return 0;

}

 

关于C++引用C函数和变量的例子还有一个(来自网上,可以google)

两个文件:

c文件:C.c
***********************************************
int external="5"; //全局变量,缺省为extern。
int func() //全局函数,缺省为extern。
{
return external;
}
***********************************************
cpp文件:CPP.cpp
***********************************************
#include "iostream"
using namespace std;
#ifdef __cplusplus
extern "C"
{
#endif
extern int external; //告诉编译器extern是在别的文件中定义的int,这里并不会为其分配存储空间。
extern int func(); //虽然这两个都是在extern "C"的{}里,但是仍然要显式指定extern,否则报错。
#ifdef __cplusplus //不仅仅是函数,变量也要放在extern "C"中。
}
#endif


void main(void)
{
cout<<"the value of external in c file is: "<external=10;
cout<<"after modified in cpp is : "<}
***********************************************

 

extern是C/C++语言中表明函数和全局变量作用范围(可见性)的关键字.,它告诉编译器,其声明的函数和变量可以在本模块或其它模块中使用。

1。对于extern变量来说,仅仅是一个变量的声明,其并不是在定义分配内存空间。如果该变量定义多次,会有连接错误

2。通常,在模块的头文件中对本模块提供给其它模块引用的函数和全局变量以关键字extern声明。也就是说c文件里面定义,如果该函数或者变量与开放给外面,则在h文件中用extern加以声明。所以外部文件只用include该h文件就可以了。而且编译阶段,外面是找不到该函数的,但是不报错。link阶段会从定义模块生成的目标代码中找到此函数。

3。与extern对应的关键字是static,被它修饰的全局变量和函数只能在本模块中使用。

 

 

本文部分内容来自:http://blog.csdn.net/vvincol/archive/2007/09/16/1787093.aspx

http://blog.csdn.net/DotScylla/archive/2009/10/11/4649401.aspx

http://blog.csdn.net/Welkin_Weng/archive/2005/12/18/555667.aspx

C/C++混合编程

前段时间,碰到了C,C++混合编程的需求,经过努力,顺利解决问题.现把这方面的知识做一下简单的总结: 1.当C++文件要用到C语言中的函数代码时,采用下属方法即可:在C++中的.h文件或.cpp文件中加入下列代码,#define LINT_ARGS 1
extern "C" {
#include "system.h"
} 然后在代码中直接调用这些函数即可.
注解:1.1 LINT_ARGS 1表示在检查C语言中的函数原型时,要对函数原型的参数进行检查. 1.2. "{ }" 中包含的头文件为C语言中的头文件.1.3.extern "C" 告诉链接器,这些头文件中的函数被当做C语言中的函数来处理. 下面以一个实例加以说明:下面为一个C语言的头文件(sysgct.h):#ifdef LINT_ARGS
  int  GCT_send(unsigned int task_id, HDR *h);
  ......#else
  int  GCT_send();  ......
#endif
~
in file MapBaseReq.cpp 文件中#include ....extern "C"
{
#include "sysgct.h"
}
void
MapBaseReq::sendPdu(const BasePdu_var& thePduP)
{    ...    if (GCT_send(m->hdr.dst, (HDR *)m) != 0)
        {
                relm((HDR *)m);
                SETERR1(errSWErrorMsg, "sendPdu(): GCT_send() Failed");
        }
   ...} 2.当C文件要用到C++语言某个类中的方法时,可以采用下列方法:2.1 在cpp文件中用下列方式定义函数:extern "C" returnType FunName(parameters list). 2.2 然后在相应的头文件中进行声明:extern returnType FunName(parameters list); 2.3 在相应的.c文件中包含该头文件,然后直接利用相应的函数即可. 下面给出实例. 2.4 cpp文件

#include
#include
#include "TTDebug.h"
using namespace std;

extern "C"
{
#include "Utility.h"
}

static int display_hex_buffer(unsigned char* buffer, unsigned char* ptr,int len);

extern "C" void tDebug_traceFunc(int level, char* trace)
{
        TDEBUG_TRACEFUNC(level,trace);
}

extern "C" void nDebug(int level, unsigned char* args, int iLen, int cid)
{
        unsigned char buf[512];
        if(0 != TTDebug::instance() && TTDebug::instance()->isTTDebugOn(level))
        {
                /* Check whether the current thread already holds the mutex lock */
                LockGuard guard(TTDebug::instance()->mutex());
                TTDebug::instance()->traceStart(level, __FILE__, __LINE__);
                memset(buf,0,512);
                display_hex_buffer(buf,args,iLen);
                TTDebug::instance()->outStream() << "Send Msg(0-0x" << setw(4) << setfill('0') << hex << cid <<"):0x" << buf;
                TTDebug::instance()->traceEnd();
        }
}

2.5 .h 文件

#ifndef __UTILITY_H
#define __UTILITY_H

extern void tDebug_traceFunc(int level, char* trace);
extern void nDebug(int level, unsigned char* args,int iLen, int cid);

#endif 2.6 cpp文件中定义的函数在c文件中调用实例在test.c文件中:

...

int ctu_ent(msg,pInt)
  MSG* msg;
  int *pInt;
{

 tDebug_traceFunc(10,"ctu ctu_ent");

  HDR *h;
  MSG *m;

   ...

}

...


posted on 2009-03-03 16:25 martin_yahoo 阅读(2551) 评论(3)  编辑 收藏 引用

评论

# re: C/C++混合编程 2009-03-03 20:44 梦在天涯

不错!很有用
在《C++编程思想》中有提到
extern "C"
{
#include "Utility.h"
}!  回复  更多评论   

# re: C/C++混合编程 2009-03-05 22:46 cdy20


c+上c头文件名,也可以
比如#include........  回复  更多评论   

# re: C/C++混合编程[未登录] 2009-03-05 23:20 martin_yahoo

@cdy20
只不过是在c头文件中已经加上了如下类似的语句:
#ifdef _c_plus_plus
extern "C"{
#endif
.....
#ifdef _c_plus_plus
}
#endif

如果在c语言的头文件中加上述语句, 就应象采用随笔中提到的做法.