gcc和g++详解

概述:本文内容都是来自于GCC官方手册,目的就是梳理清楚gcc/g++的关系,明白gcc 、g++调用的区别。

1、gcc是什么?查看GCC手册

         GCC stands for “GNU Compiler Collection”. GCC is an integrated distribution of compilers for several major programming languages. These languages currently include C, C++, Objective-C, Objective-C++, Fortran, Ada, D, Go, and BRIG (HSAIL).

         gcc正确的理解是GNU编译器合集,能编译C, C++, Objective-C, Objective-C++, Fortran, Ada, D, Go, and BRIG (HSAIL)多种语言。但是很多情况下,大家都理解成GNU C语言编译器,只用来编译C源文件,这个想法是错误的。

2、gcc只能编译C源文件?

Specify explicitly the language for the following input files (rather than letting the compiler choose a default based on the file name suffix). This option applies to all following input files until the next -x option. Possible values for languageare:

c c-header cpp-output c++ c++-header c++-cpp-output objective-c objective-c-header objective-c-cpp-output objective-c++ objective-c++-header objective-c++-cpp-output assembler assembler-with-cpp ada d f77 f77-cpp-input f95 f95-cpp-input go brig

       可使用  gcc source.file  -x language指定语言规则,来编译多种编程语言。但是默认情况下gcc只根据文件后缀名来判定源文件是什么语言,然后用对应的语法规则进行编译,用对应的链接规则生成可执行文件。

3、g++是什么?和gcc有什么关系?

查看这个:https://www.cs.bu.edu/fac/gkollios/cs113/Usingg++.html

GNU provides are a publicly-available optimizing compilers (translator) for C, C++, Ada 95, and Objective C that currently runs under various implementations of Unix (plus VMS as well as OS/2 and perhaps other PC systems) on a variety of processors too numerous to mention. You can find full documentation on-line under Emacs (use C-h i and select the ``GCC'' menu option). You don't need to know much about it for our purposes. This document is a brief summary. (Note that g++and gcc are, for most practical purposes, identical programs. Running g++ is nearly the same as gcc -lg++. Consequently, when looking at GNU's online documentation using Emacs info, gcc is where you'll look.)  Also, you can try man g++

g++在编译源文件时(C++和C都行,因为C++全兼容C语言),实质就是调用gcc按照C++语法规则来编译 ,然后自动链接C++库。所以g++  source.c编译时,会默认把这个C语言的源文件,按照C++的语法规则去编译处理。

4、g++编译C源文件会发生什么?

       随便写一个函数,保存为source.c 然后用g++ 编译 objdump反汇编,就可以看到函数名被重载处理。可以验证g++会默认把source.c以C++的语法规则去处理。

5、gcc编译c++为什么会失败!

C++ source files conventionally use one of the suffixes ‘.C’, ‘.cc’, ‘.cpp’, ‘.CPP’, ‘.c++’, ‘.cp’, or ‘.cxx’; C++ header files often use ‘.hh’, ‘.hpp’, ‘.H’, or (for shared template code) ‘.tcc’; and preprocessed C++ files use the suffix ‘.ii’. GCC recognizes files with these names and compiles them as C++ programs even if you call the compiler the same way as for compiling C programs (usually with the name gcc).

g++会把C++源文件根据C++规则编译。你使用gcc也是一样,会按照c++规则编译,所以gcc是可以编译C++的。

However, the use of gcc does not add the C++ library. g++ is a program that calls GCC and automatically specifies linking against the C++ library. It treats ‘.c’, ‘.h’ and ‘.i’ files as C++ source files instead of C source files unless -x is used. This program is also useful when precompiling a C header file with a ‘.h’ extension for use in C++ compilations. On many systems, g++ is also installed with the name c++.

使用gcc来编译c++源文件时,不会自动连接c++库,可以手动指定。g++就是调用了gcc来编译c++源文件,再自动链接c++库。所以当c++源文件引用了c++标准库函数时,使用gcc source.cpp会编译失败,因为没有自动链接c++标准库。进行手动指定后,使用gcc source.cpp -lstdc++就可以编译链接成功。

 

 

  • 5
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值