__cplusplus与extern "C"

__cplusplus与extern "C" 


Microsoft-Specific Predefined Macros 
__cplusplus Defined for C++ programs only. 
上面的意思是说,如果是C++程序,就使用(即定义__cplusplus); 


extern "C"{ 
而这句话,是指在下面的函数不使用的C++的名字修饰,而是用C的 


使用上面语句大多出现在交叉C编译环境中,其原因是: 
C++语言在编译的时候为了解决函数的多态问题,会将函数名和参数联合起来生成一个中间的函数名称,而C语言则不会,因此会造成链接时找不到对应函数的情况,此时C函数就需要用extern “C”进行链接指定,这告诉编译器,请保持我的名称,不要给我生成用于链接的中间函数名. 

__cplusplus与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 

上面的程序可解释为: 

    如果在编译时定义了__cplusplus 
那么编译器编译的代码文本就是: 

       extern "C" { 
    int sum(int num1,int num2);     
    int mult(int num1,int num2); 
                   } 

    如果编译时没有定义__cplusplus 

    就是: 

    int sum(int num1,int num2);     
    int mult(int num1,int num2);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值