gcc和g++的区别

来源:互联网 发布:bartender vb脚本编写 编辑:程序博客网 时间:2024/06/06 05:53
参考1:http://www.linuxsky.org/doc/dev/200804/298.html

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,二者等价。

参考2:http://en.wikipedia.org/wiki/GNU_Compiler_Collection

The GNU Compiler Collection (GCC) is a compiler system produced by the GNU Project supporting various programming languages.

Originally named the GNU C Compiler, because it only handled the C programming language, GCC 1.0 was released in 1987, and the compiler was extended to compile C++ in December of that year.[1] Front endswere later developed for Objective-C, Objective-C++, Fortran, Java, Ada, and Go among others.[8]

参考3:An introduction to GCC
这本书中,
1.1节,The original author of the GNU C Compiler (GCC) is Richard Stallman,the founder of the GNU Project.Over time GCC has been extended to support many additional lan-guages, including Fortran, ADA, Java and Objective-C. The acronym GCC is now used to refer to the “GNU Compiler Collection”
1.3节中,In addition to C and C++the GNU Project also provides other high-level languages, such as GNU Common Lisp (gcl), GNU Smalltalk (gst), the GNU Scheme extension language (guile) and the GNU Compiler for Java (gcj). These languages do not allow the user to access memory directly, eliminating the possibility of memory access errors. They are a safer alternative to C and C++for many applications.

7.1节中,The procedure for compiling a C++ program is the same as for a C pro-gram, but uses the command g++ instead of gcc. Both compilers are part of the GNU Compiler Collection.

综上所述,我还是按照gcc编译c程序,g++来编译c++程序来使用GCC吧!

原创粉丝点击