gcc和g++的区别

简介:

 gccg++的区别

Author: Chaos Lee

Date: 2012/04/28

         首先,是名字上的区别(废话?)。gcc开始时候的名字是gnu c compiler 就是说设计的初衷是用来编译C语言的。 后来,不断的拓展发展成了 gnu compiler collection. 如果你用gcc编译过fortran代码的话,就会对后者理解的比较深刻了。现在的gcc可以看做是gnu支持的语言编译器的前端。g++的设计目标是用来编译C++程序代码的,如期名字所暗示的那样。因此,g++是一个c++ compilergcc compiler collection. 所以,可以推知g++的功能只是gcc的一个子集。

         gcc可以用来编译.cpp为后缀的c++程序源文件,同样g++也可以。你可能以为使用gcc编译的时候,会调用g++。然而,并非如此!!!g++诞生的比gcc晚,刚诞生的g++事实上是用脚本语言写的,其中将一系列的命令行参数传给了gcc,所以应该说g++的内部调用了gcc。就版本的g++中,g++的实现脚本是用bash写的。虽然现在的g++使用二进制可执行文件写的,但是内部原理还是一样的。

         gccg++的具体区别有以下几点:

l  对于.cpp为后缀的C++文件,使用gcc编译或者g++编译效果差不多一样的,但是连接的时候不同,g++会在链接的时候自动使用libstdc++,而gcc不会。

以下为实验代码:

 
  1. /* 
  2.  
  3. * lib.cpp 
  4.  
  5. * a demo to show the difference between g++ and gcc , 
  6.  
  7. * when used to compile .cpp source files. 
  8.  
  9. * Author:Chaos Lee 
  10.  
  11. */ 
  12.  
  13. #include<iostream> 
  14.  
  15. using namespace std; 
  16.  
  17. int main(int argc,char *argv[]) 
  18.  
  19.  
  20.         cout<<"Hello world.\n"
  21.  
  22.         return 0; 
  23.  
  24. }       

          

                   接下来使用gcc编译:

                 

 
  1.   [lichao@sg01 gccORg++]$ gcc lib.cpp -o lib 
  2.  
  3. /tmp/ccJLFgP9.o: In function `__static_initialization_and_destruction_0(int, int)': 
  4.  
  5. lib.cpp:(.text+0x23): undefined reference to `std::ios_base::Init::Init()' 
  6.  
  7. /tmp/ccJLFgP9.o: In function `__tcf_0': 
  8.  
  9. lib.cpp:(.text+0x66): undefined reference to `std::ios_base::Init::~Init()' 
  10.  
  11. /tmp/ccJLFgP9.o: In function `main': 
  12.  
  13. lib.cpp:(.text+0x81): undefined reference to `std::cout' 
  14.  
  15. lib.cpp:(.text+0x86): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)' 
  16.  
  17. /tmp/ccJLFgP9.o:(.eh_frame+0x12): undefined reference to `__gxx_personality_v0' 
  18.  
  19. collect2: ld returned 1 exit status 

用红色标示的部分标示链接器提示的错误,可以看出都是引用错误。接下来使用-lstdc++测试一下:

 

 
  1. [lichao@sg01 gccORg++]$ gcc lib.cpp -o lib -lstdc++ 
  2.  
  3. [lichao@sg01 gccORg++]$ ./lib 
  4.  
  5. Hello world. 
  6.  
  7. OK. 

在使用g++测试一下:

 

 
  1. [lichao@sg01 gccORg++]$ g++ lib.cpp -o lib 
  2.  
  3. [lichao@sg01 gccORg++]$ ./lib 
  4.  
  5. Hello world. 

由此验证上述结论。

l  对于.c为后缀的源文件,gcc默认使用c编译器去编译,而g++默认调用的是c++的编译器。

这里可以做一个简单的实验,用宏来测试一下:

测试代码为:

 

 
  1. /* 
  2.  
  3. * macro.c 
  4.  
  5. * a demo to show the difference between g++ and gcc, when used to compile 
  6.  
  7. * .c source files. 
  8.  
  9. * Author: Chaos Lee 
  10.  
  11. */ 
  12.  
  13. #include<stdio.h> 
  14.  
  15. int main(int argc,char *argv) 
  16.  
  17.  
  18. #ifdef __cplusplus 
  19.  
  20.         printf("compiling with c++ compiler\n"); 
  21.  
  22. #else 
  23.  
  24.         printf("compiling with c compiler.\n"); 
  25.  
  26. #endif 
  27.  
  28.         return 0; 
  29.  

以下分别使用gccg++来测试(先验了一下,哈哈):

 

 
  1. [lichao@sg01 gccORg++]$ gcc macro.c -o macro 
  2.  
  3. [lichao@sg01 gccORg++]$ ./macro 
  4.  
  5. compiling with c compiler. 
  6.  
  7. [lichao@sg01 gccORg++]$ g++ macro.c -o macro 
  8.  
  9. [lichao@sg01 gccORg++]$ ./macro 
  10.  
  11.          compiling with c++ compiler 

         由此验证上述结论。

l  使用gccg++在编译.cpp文件时都会额外定义一些宏,这些宏在使用gcc编译.c文件时没有的,这些宏包括:

   

 
  1.  #define __GXX_WEAK__ 1 
  2.  
  3. #define __cplusplus 1 
  4.  
  5. #define __DEPRECATED 1 
  6.  
  7. #define __GNUG__ 4 
  8.  
  9. #define __EXCEPTIONS 1 
  10.  
  11. #define __private_extern__ extern 
  12.  
  13.   

 


本文转自hipercomer 51CTO博客,原文链接:http://blog.51cto.com/hipercomer/846923


相关文章
|
4月前
|
NoSQL 编译器 开发工具
006.gcc编译器
gcc是什么?
47 0
006.gcc编译器
|
5月前
|
存储 NoSQL 算法
从一个crash问题展开,探索gcc编译优化细节
问题分析的过程也正是技术成长之路,本文以一个gcc编译优化引发的crash为切入点,逐步展开对编译器优化细节的探索之路,在分析过程中打开了新世界的大门……
434 1
|
21天前
|
C语言
转载 - gcc/ld 动态连接库和静态连接库使用方法
本文介绍了如何在GCC中实现部分程序静态链接、部分动态链接。使用`-Wl`标志传递链接器参数,`-Bstatic`强制链接静态库,`-Bdynamic`强制链接动态库。
31 0
|
2月前
|
编译器 C语言 C++
列举gcc 常见和有用的编译警告选项
列举gcc 常见和有用的编译警告选项
13 0
|
2月前
|
编译器 C语言
gcc编译警告:warning: suggest parentheses around assignment used as truth value
gcc编译警告:warning: suggest parentheses around assignment used as truth value
24 0
|
2月前
|
编译器 Linux C语言
gcc编译器的使用方法
gcc编译器的使用方法
23 1
|
3月前
|
编译器 C语言
gcc/g++语法
gcc/g++语法
|
5月前
|
C语言
gcc静态编译/usr/bin/ld: cannot find -lc
gcc静态编译/usr/bin/ld: cannot find -lc
|
6月前
|
编译器 程序员 C语言
gcc的编译过程和gcc与g++的区别
gcc的编译过程和gcc与g++的区别
55 0
|
7月前
|
C语言
编译安装gcc
编译安装gcc