Chinaunix首页 | 论坛 | 博客
  • 博客访问: 294419
  • 博文数量: 42
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 847
  • 用 户 组: 普通用户
  • 注册时间: 2013-02-28 14:14
个人简介

北冥有鱼,其名曰鲲,鲲之大,不知其几千里也;化而为鸟,其名为鹏,鹏之背,不知其几千里也,怒而飞,其翼若垂天之云。

文章分类

全部博文(42)

文章存档

2015年(6)

2014年(7)

2013年(29)

分类: LINUX

2015-08-12 19:58:35

原文地址:linux下C与C++混合编程 作者:yandaren_1220

首先,混合编程不是指在同一个文件里写C与C++。 比如说想在同意文件里同时 弱类型检查,又强类型检查,那真够呛。

混合编程包括:1,C++引用C的头文件;2,g++生成的.o与gcc生成的.o相链接。

1,在用C语言写的头文件中,经常加上如下 保护宏定义:

  1. /*
  2.     example.h
  3. */
  4. #ifndef EXAMPLE_H_
  5. #define EXAMPLE_H_
  6. #ifdef __cplusplus
  7. extern "C"{
  8. #endif
  9. /*这里是头文件内容*/
  10. /*头文件内容结束*/
  11. #ifdef __cplusplus
  12. }
  13. #endif
  14. #endif

2,关于extern "C"

          当c++引用c的函数,结构体定义等时,要声明extern "C"  从某种意义上,这倒是像是在C++文件里写C的一种方法。事实上,由于c++会将函数标示符进行修饰后使用,而C不会,所以用C++编译的fun有可能是fun_int,这样在链接时会出现问题。使用extern “C”来解决这一问题,但带来的影响是不能重载了。

         总之,extern "C"即叫编译器按照C的方式去处理。

3,具体编译命令 g++ 与 gcc

实例1:

  1. //test.c

  2. int fun(int a, int b)

  3. {

  4.       return a+b;

  5. }

  6. //main.cpp

  7. #include <stdio.h>

  8. #include "test.c"

  9. int main()

  10. {

  11.       printf("%d\n", fun(10, 11));

  12.       return 0;

  13. }

首先理解include,include会把包含的文件直接加在本文件中,和copy过来一样。而我们直接包含了test.c文件,而不是头文件,

所以,直接:   g++ -o main main.cpp  即可得到可执行文件。

实例2:

  1. //test.c
  2. int fun(int a, int b)
  3. {
  4.       return a+b;
  5. }
  6. //test.h
  7. int fun(int, int);
  8. //main.cpp
  9. #include <stdio.h>
  10. #include "test.h"
  11. int main()
  12. {
  13.       printf("%d\n", fun(10, 11));
  14.       return 0;
  15. }

正确的编译方法:

            g++ -c test.c                 //生成test.o

            g++ -c main.cpp          //生成main.cpp

            g++ -o main test.o main.o  //链接,生成main可执行文件

错误的编译方法:

            gcc -c test.c                 //生成test.o

            g++ -c main.cpp          //生成main.cpp

            g++ -o main test.o main.o  //链接,生成main可执行文件

   如果,想使第二种方法正确,则在test.h中使用extern “C”声明,或者在main.cpp中,使用extern "C"声明

实例3

正确写法1

  1. //test.c
  2. int fun(int a, int b)
  3. {
  4.       return a+b;
  5. }
  6. //test.h
  7. #ifdef __cplusplus
  8. extern "C"{
  9. #endif
  10. int fun(int, int);
  11. #ifdef __cplusplus
  12. }
  13. #endif
  14. //main.cpp
  15. #include <stdio.h>
  16. #include "test.h"
  17. int main()
  18. {
  19.       printf("%d\n", fun(10, 11));
  20.       return 0;
  21. }

正确写法2

  1. //test.c
  2. int fun(int a, int b)
  3. {
  4.      return a+b;
  5. }
  6. //test.h
  7. int fun(int, int);
  8. //main.cpp
  9. #include <stdio.h>
  10. extern "C"
  11. {
  12.      #include "test.h"
  13. }
  14. int main()
  15. {
  16.       printf("%d\n", fun(10, 11));
  17.       return 0;
  18. }

正确写法3

  1. //test.c
  2. int fun(int a, int b)
  3. {
  4.       return a+b;
  5. }
  6. //test.h
  7. int fun(int, int);
  8. //main.cpp
  9. #include <stdio.h>
  10. extern "C" int fun(int, int)
  11. int main()
  12. {
  13.       printf("%d\n", fun(10, 11));
  14.       return 0;
  15. }

其中正确写法3很有意思,在main.cpp中,并没有包含test.h, 这里写的extern "C" int fun(int, int),其实就是头文件内容。把头文件内容人工手写在main.cpp中和用include包含进来,是一样效果,这样就好理解了。 include“test.h” ,其实也就是写了一句 extern "C" int fun(int, int)。

所以严格来说,.h文件无所谓是属于C还是C++,被谁包含,就属于那种语言。

4, 关于g++,gcc

直接引用http://blog.ednchina.com/olivernie/161559/message.aspx上的原文,这段话是转载,不是我写的。

在国内搜索引擎搜索gcc与g++,大多都是在说g++会调用gcc来编译c++文件,国外stackoverflow大多再说gcc调用g++。

有争议,先mark

gcc和g++都是GNU(组织)的一个编译器。  
   
  误区一 :    gcc只能编译c代码,g++只能编译c++代码  
  两者都可以,但是请注意:  
  1.后缀为.c的,gcc把它当作是C程序,而g++当作是c++程序;后缀为.cpp的,两者都会认为是c++程序,注意,虽然c++是c的超集,但是两者对语法的要求是有区别的。C++的语法规则更加严谨一些。  
  2.编译阶段,g++会调用gcc,对于c++代码,两者是等价的,但是因为gcc命令不能自动和C++程序使用的库联接,所以通常用g++来完成链接,为了统一起见,干脆编译/链接统统用g++了,这就给人一种错觉,好像cpp程序只能用g++似的。  
     
  误区二  :  gcc不会定义__cplusplus宏,而g++会  
  实际上,这个宏只是标志着编译器将会把代码按C还是C++语法来解释,如上所述,如果后缀为.c,并且采用gcc编译器,则该宏就是未定义的,否则,就是已定义。  
     
  误区三 :   编译只能用gcc,链接只能用g++  
  严格来说,这句话不算错误,但是它混淆了概念,应该这样说:编译可以用gcc/g++,而链接可以用g++或者gcc   -lstdc++。因为gcc命令不能自动和C++程序使用的库联接,所以通常使用g++来完成联接。但在编译阶段,g++会自动调用gcc,二者等价。

用gcc编译c++文件:
#gcc -o test.exe test.cpp -lstdc++

阅读(2039) | 评论(0) | 转发(0) |
0

上一篇:R语言循环补齐功能

下一篇:没有了

给主人留下些什么吧!~~