混合编译:c++文件调用c文件变量和函数、c文件调用c++文件变量和函数

来源:互联网 发布:sql中update的用法 编辑:程序博客网 时间:2024/06/09 21:24

1.c++文件调用c文件变量和函数

前言:以前见到extern "C"这样的语句,只是简单地知道跟外部链接有关,但是没有深刻理解它的意思。

首先,为什么要使用extern "C"修饰符?

C++调用其它语言的函数,由于编译器生成函数的机制不一样,所以需要经过特殊处理,才可以调用。调用C语言的函数,需要在函数声明的地方语句extern "C"。如果不使用该语句,在链接的时候,编译器就会报以下这种错误。

Test.obj : error LNK2019: 无法解析的外部符号 "void __cdecl DeleteStack(struct _Node *)" (?DeleteStack@@YAXPAU_Node@@@Z),该符号在函数 _main 中被引用。


然后是如何使用?

应该怎么使用该语句呢?

刚开始,我简单地在C++源文件的前面使用该语句声明,但是还是出错,而且是在编译阶段就报错。

[cpp] view plain copy
  1. error C2732: 链接规范与“DeleteStack”的早期规范冲突。  

为什么会出现这个错误呢?因为C++源文件已经引入了C的头文件,在头文件里,声明该函数时没有extern修饰,而这里有extern修饰,所以冲突了。解决的办法有两个。

一。在C头文件中加上extern修饰符。

直接加,也不行。因为C源文件也包含了这个头文件,当编译C源文件时,就会出现错误。所以,需要一种机制来区分是编译C还是C++文件。方法如下:

[cpp] view plain copy
  1. #ifdef __cplusplus  
  2. extern "C"  
  3. #endif  
  4.  void DeleteStack(Stack stack);  

因为在编译C++文件时,自动定义预处理器名字__cplusplus,而编译C时,没有该处理器名字。所以只有编译C++时,才有符号extern “C”。

此外,链接指示extern "C"有单个和复合两种形式。以上为单个形式,复合形式可以同时将几个函数声明为extern "C"

[cpp] view plain copy
  1. extern "C" {  
  2. void DeleteStack(Stack stack);  
  3. void PrintStack(Stack stack);  
  4. void Pop(Stack stack);  
  5. }  

加上预处理器名字如下:
[cpp] view plain copy
  1. #ifdef __cplusplus  
  2. extern "C" {  
  3. #endif  
  4.   
  5. void DeleteStack(Stack stack);  
  6. void PrintStack(Stack stack);  
  7. void Pop(Stack stack);  
  8.   
  9. #ifdef __cplusplus  
  10. }  
  11. #endif  

二。编写一个C++风格的头文件,在这里添加extern修饰符。

使用方法一,很简单。但是如果该头文件是别人写好,你无法修改。这个时候就要使用其它方法了。方法是定义C++自己的头文件,文件名为"CStack.h"

[cpp] view plain copy
  1. // CStack.h  
  2. extern "C" {  
  3. #include "Stack.h";  

2.c文件调用c++文件变量和函数

//xxx.cpp

#ifdef __cplusplus  
extern "C" {
#endif

int ignore_raw_data;

#ifdef __cplusplus  
}  
#endif

//xxx.c

extern int ignore_raw_data;

3.综合使用
一般我们都将函数声明放在头文件,当我们的函数有可能被C或C++使用时,但我们无法确定函数是在c还是cpp里面被定义(cxx都算,如苹果是mm,google是cc),也就无法确定是否要将函数声明在extern "C"里,所以,我们应该添加
xxx.h

[cpp] view plain copy print?
  1. #ifdef __cplusplus  
  2. extern "C"  
  3. {  
  4. #endif  
  5. //函数声明  
  6. #ifdef __cplusplus  
  7. }  
  8. #endif  

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

[cpp] view plain copy print?
  1. //test.h  
  2. #ifdef __cplusplus  
  3. #include <iostream>  
  4. using namespace std;  
  5. extern "C"  
  6. {  
  7. #endif  
  8. void mytest();  
  9. #ifdef __cplusplus  
  10. }  
  11. #endif  

这样,可以将mytest()的实现放在.c或者.cpp文件中,可以在.c或者.cpp文件中include "test.h"后使用头文件里面的函数,而不会出现编译错误。一般情况下,只需要把_cpluscplus和extern "C"的关键字放到h文件就行了,c文件和cpp文件包含这个头文件,而不需要在加入这两个关键字,以下是特殊情况,因为cout不能再c里面用。

[cpp] view plain copy print?
  1. //test.c或者test.cpp  
  2. #include "test.h"  
  3. void mytest()  
  4. {  
  5. #ifdef __cplusplus  
  6. cout << "cout mytest extern ok " << endl;  
  7. #else  
  8. printf("printf mytest extern ok n");  
  9. #endif  
  10. }  
[cpp] view plain copy print?
  1. //main.cpp  
  2. #include "test.h"  
  3. int main()  
  4. {  
  5. mytest();  
  6. return 0;  
  7. }  


总之,以后再见到这样的用法就不要大惊小怪了。


参考:

http://blog.csdn.net/u012234115/article/details/43272441

http://bbs.csdn.net/topics/380061545

http://www.cnblogs.com/rollenholt/archive/2012/03/20/2409046.html


0 0