C++与C变量或函数的混合调用

来源:互联网 发布:手机淘宝上买二手 编辑:程序博客网 时间:2024/05/22 14:00

第一部分:C++调用C变量或函数

       如果我想在C文件中实现某些功能,在CPP文件实现对这些功能的调用,我该如何做呢?

先将程序贴出来,然后在分析:

[cpp] view plaincopy
  1. // file name : inct.h  
  2. #ifndef _INCT_H_  
  3. #define _INCT_H_  
  4.   
  5. #define NUM 8  
  6.   
  7. #ifdef __cplusplus  
  8. extern "C" {  
  9. #endif 

  10. // global C  
  11. extern int g_data[4][NUM]; /////c中的全局变量
  12.   
  13. // function  
  14. int* func(int n);  /////c中的函数
  15.   
  16. #ifdef __cplusplus  
  17. }  
  18. #endif  
  19.   
  20. #endif  

[cpp] view plaincopy
  1. //file name : inct.c  
  2. #include "inct.h"  
  3. #include <stdlib.h>  
  4. #include <stdio.h>  
  5.   
  6. int g_data[4][NUM]= \  
  7. { \  
  8.   {  0, 0,   0, 64,  0,   0, 0,  0 },\  
  9.   { -1, 4, -10, 58, 17,  -5, 1,  0 },\  
  10.   { -1, 4, -11, 40, 40, -11, 4, -1 },\  
  11.   {  0, 1,  -5, 17, 58, -10, 4, -1 }\  
  12. };\  
  13.   
  14. int* func(int n)  
  15. {  
  16.     printf("your input is %d.\n", n);  
  17.     return (int*)malloc(n*sizeof(int));  
  18. }  

[cpp] view plaincopy
  1. // file name : test.cpp  
  2. #include <iostream>  
  3. #include <string>  
  4. #include <cstring>  
  5. #include "inct.h"  
  6. #include <stdlib.h>  
  7. #include <stdio.h>  
  8. using namespace std;  
  9.   
  10. int main(int argc, char **argv)  
  11. {  
  12.     int n=NUM;  
  13.     int *data = func(n);  
  14.     for (int i = 0; i < n; i++)  
  15.     {  
  16.         data[i] = g_data[2][i];  
  17.         printf("data[%d]=%4d\n", i, data[i]);  
  18.     }  
  19.   
  20.     free(data);  
  21.   
  22.     return 0;  
  23. }  


我将这样编译:

1、首先将inct.c编译:

[cpp] view plaincopy
  1. gcc -c -I. inct.c  

此编译将会输出:inct.o

执行过程分析:gcc编译时,并没有预先定义__cplusplus这个宏,所以extern "C"并没有有效,inct.h被作为一般的C程序编译成功。


2、将test.cpp用C++编译器编译:

[cpp] view plaincopy
  1. g++ -c -I. test.cpp  

输出 test.o

g++中定义了__cplusplus,所以,test.cpp中对inct.h中所有功能引用都添加了extern “C”,这样一来,c++中的程序引用.h中的东西时,就将他们看成由C编译器编译成的程序了。编译也没有问题。


3、将所有编译中间结果链接起来

[cpp] view plaincopy
  1. g++ test.o inct.o -o test  









第二部分:C中调用C++的函数或变量

先贴代码:

inct.h的内容没有改变。

将inct.c改为inct.cpp,并且修改下程序内容:

[cpp] view plaincopy
  1. //file name : inct.c  
  2. #include "inct.h"  
  3. #include <iostream>  
  4. using namespace std; //使用std命名空间  
  5.   
  6. int g_data[4][NUM]= \  
  7. { \  
  8.   {  0, 0,   0, 64,  0,   0, 0,  0 },\  
  9.   { -1, 4, -10, 58, 17,  -5, 1,  0 },\  
  10.   { -1, 4, -11, 40, 40, -11, 4, -1 },\  
  11.   {  0, 1,  -5, 17, 58, -10, 4, -1 }\  
  12. };\  
  13.   
  14. int* func(int n)  
  15. {  
  16.     cout << "your input is " << n << endl; //使用了cout  
  17.     return (new int[n]); // 使用了new  
  18.     //return (int*)malloc(n*sizeof(int));  
  19. }  

将test.cpp修改为test.c,内容改变为:
[cpp] view plaincopy
  1. // file name : test.c  
  2. #include "inct.h"  
  3. #include <stdlib.h>   
  4. #include <stdio.h> //嫣然都是C的头文件  
  5.   
  6. int main(int argc, char **argv)  
  7. {  
  8.     int n=NUM;  
  9.     int *data = func(n);  
  10.     int i; //将i的声明拿出来了,C的语法  
  11.     for (i = 0; i < n; i++)  
  12.     {  
  13.         data[i] = g_data[2][i];  
  14.         printf("data[%d]=%4d\n", i, data[i]); 继续使用printf,来自C的接口  
  15.     }  
  16.   
  17.     free(data); //将new来的数据free掉  
  18.     return 0;  
  19. }  


这样编译:

1、首先将inct.cpp编译:

[cpp] view plaincopy
  1. g++ -c -I. inct.cpp  

此编译将会输出:inct.o

执行过程分析:g++编译时,预先定义__cplusplus这个宏,所以extern "C"有效,inct.h被作为一般的C++程序编译成功,但是编译后的变量和函数可以在C或C++程序中使用。


2、将test.c用C编译器编译:

[cpp] view plaincopy
  1. gcc -c -I. test.c  

输出 test.o

gcc中没定义__cplusplus,所以,test.c中对inct.h中所有功能引用都没添加了extern “C”,这样一来,C语言程序正常引用这些函数或变量。

特别注意 extern "C"是C++中才有的,在C中只有extern,用在变量前,表示变量的声明,不表示变量的定义。


3、将所有编译中间结果链接起来

[cpp] view plaincopy
  1. g++ test.o inct.o -o test  
或者
[cpp] view plaincopy
  1. gcc test.o inct.o -o test -lstdc++  
0 0
原创粉丝点击