Linux下g++和gcc之间的区别

gcc和g++都是GNU的一个编译器;
这两者的区别:
1.从源文件上看,对于文件后缀(扩展名)为.c的test.c文件,gcc会把它看成是C程序,而g++则会把它看成是C++程序;而对于文件后缀(扩展名)为.cpp的test.cpp文件,gcc和g++都会把它看成是C++程序;注意:虽然C++是C的超集,但是两者在语法要求上还是有区别的,C++的语法要求更严谨一些;
2.从编译器角度看,在编译阶段,g++会自动调用gcc,对于编译C++代码,两者是等价的,但是由于gcc不会自动调用C++程序所使用的库进行链接,所以需要使用g++来编译或者是在gcc的命令行加上对C++库的链接-lstdc++;
3.gcc和g++对宏__cplusplus的处理:实际上这个宏是标志着编译器将会把代码按照C的语法来解释还是按照C++的语法来编译,如上所述,如果源文件的扩展名是.c,并且使用gcc编译,那么宏__cplusplus将是未定义的,否则,就是已定义的;
4.extern "C"的功能就就是把它所界定的那些函数按照C语言的语法和规则来编译;这是一个函数调用约定;
5.使用extern "C"与使用gcc和g++并没有关系;因为extern "C"只是用来约束代码按照C语言的语法要求和规则来编译;无论是gcc还是g++,使用extern "C"来约束的时候,都是以C语言的命名方式来为symbol命名的,否则,都是以C++语言的命名方式来为symbol命名的;

实验一:test.cpp
#include <stdio.h>
#include <string.h>

int main(int argc, char** argv)
{
  if(argv == 0) return ;
  printString(argv);
  printf("%s", argv);
  return;
}

int printString(char* string)
{
  sprintf(string, "this is test\n");
}
用gcc和g++编译:
-bash-3.00$ gcc -o test test.cpp
test.cpp: In function `int main(int, char**)':
test.cpp:7: error: return-statement with no value, in function returning 'int'
test.cpp:8: error: `printString' was not declared in this scope
test.cpp:10: error: return-statement with no value, in function returning 'int'
都报这样的错误;
因为源文件的扩展名是.cpp,所以,gcc和g++编译器都把test.cpp当做是C++程序;

把test.cpp重命名为test.c;
先用g++编译:
-bash-3.00$ g++ -o test test.c
test.c: In function `int main(int, char**)':
test.c:7: error: return-statement with no value, in function returning 'int'
test.c:8: error: `printString' was not declared in this scope
test.c:10: error: return-statement with no value, in function returning 'int'
仍然抱这样的错误:因为g++把test.c看成是C++程序,依照C++语言的语法要求来编译;
再用gcc编译:
-bash-3.00$ gcc -o test test.c
-bash-3.00$
没有报错:因为gcc把test.c看成是C程序,依照C语言的语法要求来编译;
结论:
这两次实验充分说明,A:对于一个扩展名为.c的代码,gcc会把它看成是C程序,依照C语言的语法要求来编译,而g++会把它看成C++程序,并依照C++语言的语法要求来编译;而对于一个扩展名为.cpp的代码,gcc和g++都会把他看成C++程序,按照C++的语法要求来编译;B:C++语言的语法要求比C语言的语法要求更严谨;

实验二:extern "C"与使用gcc/g++的关系(没有任何关系):
#include <stdio.h>
#include <string.h>

extern "C" int printString(char*);

int main(int argc, char** argv)
{
  char str[32];
  if(argv == 0) return -1;
  printString(str);
  printf("%s", str);
  return 0;
}

int printString(char* string)
{
  sprintf(string, "this is test\n");
  return 0;
}
-bash-3.00$ gcc -S test.cpp
-bash-3.00$ less test.s | grep -i printString
        call    printString, 0
        .global printString            <------函数的命名
        .type   printString, #function      <----Solaris平台时是"#function",Linux平台时是"@function";
printString:
        .size   printString, .-printString
-bash-3.00$
-bash-3.00$ g++ -S test.cpp
-bash-3.00$ less test.s | grep -i printString
        call    printString, 0
        .global printString            <------函数的命名
        .type   printString, #function      <----Solaris平台时是"#function",Linux平台时是"@function";
printString:
        .size   printString, .-printString
-bash-3.00$
使用gcc与使用g++完全相同;

去掉extern "C"之后再编译:
-bash-3.00$ gcc -S test.cpp
-bash-3.00$ less test.s | grep -i printString
        call    _Z11printStringPc, 0
        .global _Z11printStringPc      <------函数的命名
        .type   _Z11printStringPc, #function     <----Solaris平台时是"#function",Linux平台时是"@function";
_Z11printStringPc:
        .size   _Z11printStringPc, .-_Z11printStringPc
-bash-3.00$
-bash-3.00$ g++ -S test.cpp
-bash-3.00$ less test.s | grep -i printString
        call    _Z11printStringPc, 0
        .global _Z11printStringPc      <------函数的命名
        .type   _Z11printStringPc, #function     <----Solaris平台时是"#function",Linux平台时是"@function";
_Z11printStringPc:
        .size   _Z11printStringPc, .-_Z11printStringPc
-bash-3.00$
使用gcc与使用g++完全相同;
结论:
A.使用extern "C"与使用编译器gcc还是g++没有关系;extern "C"的使用只是约定代码按照C语言的语法要求来编译;extern "C"的使用独立于编译器的选择;换句话说,无论使用gcc还是g++,如果使用extern "C"来约定函数调用,则以C语言命名规则来命名;如果不使用extern "C"来约定函数调用,则以C++语言的命名规则来命名;
B.在编译阶段,g++是要调用gcc的;

 

本文转自:http://bdxnote.blog.163.com/blog/static/8444235201001701236417/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值