简体   繁体   English

g++和gcc有什么区别?

[英]What is the difference between g++ and gcc?

What is the difference between g++ and gcc? g++和gcc有什么区别? Which one of them should be used for general c++ development?其中哪一个应该用于一般的 c++ 开发?

gcc and g++ are compiler-drivers of the GNU Compiler Collection (which was once upon a time just the GNU C Compiler ). gccg++是 GNU Compiler Collection (曾是 GNU C Compiler )的编译器驱动程序。

Even though they automatically determine which backends ( cc1 cc1plus ...) to call depending on the file-type, unless overridden with -x language , they have some differences.即使它们根据文件类型自动确定要调用的后端( cc1 cc1plus ...),除非用-x language覆盖,它们还是有一些区别。

The probably most important difference in their defaults is which libraries they link against automatically.它们的默认值可能最重要的区别是它们自动链接到哪些库。

According to GCC's online documentation link options and how g++ is invoked , g++ is equivalent to gcc -xc++ -lstdc++ -shared-libgcc (the 1st is a compiler option, the 2nd two are linker options).根据 GCC 的在线文档链接选项g++ 的调用方式g++相当于gcc -xc++ -lstdc++ -shared-libgcc (第一个是编译器选项,第二个是链接器选项)。 This can be checked by running both with the -v option (it displays the backend toolchain commands being run).这可以通过使用-v选项同时运行来检查(它显示正在运行的后端工具链命令)。

GCC: GNU Compiler Collection GCC:GNU 编译器集合

  • Referrers to all the different languages that are supported by the GNU compiler. GNU 编译器支持的所有不同语言的引用。

gcc : GNU C Compiler gcc : GNU C 编译器
g++ : GNU C++ Compiler g++ :GNU C++ 编译器

The main differences:主要区别:

  1. gcc will compile: *.c\*.cpp files as C and C++ respectively. gcc将编译: *.c\*.cpp文件分别为 C 和 C++。
  2. g++ will compile: *.c\*.cpp files but they will all be treated as C++ files. g++将编译: *.c\*.cpp文件,但它们都将被视为 C++ 文件。
  3. Also if you use g++ to link the object files it automatically links in the std C++ libraries ( gcc does not do this).此外,如果您使用g++链接目标文件,它会自动链接到 std C++ 库( gcc不这样做)。
  4. gcc compiling C files has fewer predefined macros. gcc编译 C 文件的预定义宏较少。
  5. gcc compiling *.cpp and g++ compiling *.c\*.cpp files has a few extra macros. gcc编译*.cppg++编译*.c\*.cpp文件有一些额外的宏。

Extra Macros when compiling *.cpp files:编译*.cpp文件时的额外宏:

#define __GXX_WEAK__ 1
#define __cplusplus 1
#define __DEPRECATED 1
#define __GNUG__ 4
#define __EXCEPTIONS 1
#define __private_extern__ extern

For c++ you should use g++.对于 c++,你应该使用 g++。

It's the same compiler (eg the GNU compiler collection).它是同一个编译器(例如 GNU 编译器集合)。 GCC or G++ just choose a different front-end with different default options. GCC 或 G++ 只是选择具有不同默认选项的不同前端。

In a nutshell: if you use g++ the frontend will tell the linker that you may want to link with the C++ standard libraries.简而言之:如果你使用 g++,前端会告诉链接器你可能想链接 C++ 标准库。 The gcc frontend won't do that (also it could link with them if you pass the right command line options). gcc 前端不会这样做(如果您传递正确的命令行选项,它也可以与它们链接)。

What is the difference between g++ and gcc ? g++gcc有什么区别?

gcc has evolved from a single language "GNU C Compiler" to be a multi-language "GNU Compiler Collection". gcc已经从单一语言“GNU C 编译器”演变为多语言“GNU 编译器集合”。 The term gcc may still sometimes refer to the "GNU C Compiler" in the context of C programming.在 C 编程的上下文中,术语gcc有时可能仍指代“GNU C 编译器”。

man gcc

# GCC(1)                     GNU
# 
# NAME
#        gcc - GNU project C and C++ compiler

However, g++ is the C++ compiler for the GNU Compiler Collection.但是, g++GNU Compiler Collection 的 C++ 编译器。 Like gnat is the Ada compiler for gcc .gnatgcc的 Ada 编译器。 see Using the GNU Compiler Collection (GCC) 请参阅使用 GNU 编译器集合 (GCC)

For example, the Ubuntu 16.04 and 18.04 man g++ command returns the GCC(1) manual page.例如,Ubuntu 16.04 和 18.04 man g++命令返回GCC(1)手册页。

The Ubuntu 16.04 and 18.04 man gcc states that ... Ubuntu 16.04 和 18.04 的man gcc声明...

g++ accepts mostly the same options as gcc g++接受与gcc几乎相同的选项

and that the default ...并且默认...

... use of gcc does not add the C++ library. ... 使用gcc不会添加 C++ 库。 g++ is a program that calls GCC and automatically specifies linking against the C++ library. g++是一个调用 GCC 并自动指定与 C++ 库的链接的程序。 It treats .c, .h and .i files as C++ source files instead of C source files unless -x is used.除非使用 -x,否则它将 .c、.h 和 .i 文件视为 C++ 源文件而不是 C 源文件。 This program is also useful when precompiling a C header file with a .h extension for use in C++ compilations.在预编译扩展名为 .h 的 C 头文件以用于 C++ 编译时,此程序也很有用。

Search the gcc man pages for more details on the option variances between gcc and g++ .搜索gcc手册页以获取有关gccg++之间选项差异的更多详细信息。

Which one should be used for general c++ development?哪一个应该用于一般的 c++ 开发?

Technically, either gcc or g++ can be used for general C++ development with applicable option settings.从技术上讲, gccg++都可用于具有适用选项设置的一般 C++ 开发。 However, the g++ default behavior is naturally aligned to a C++ development.但是, g++默认行为自然与 C++ 开发保持一致。

The Ubuntu 18.04 'gcc' man page added, and Ubuntu 20.04 continues to have, the following paragraph:添加了Ubuntu 18.04 'gcc' 手册页,并且Ubuntu 20.04继续具有以下段落:

The usual way to run GCC is to run the executable called gcc , or machine-gcc when cross-compiling, or machine-gcc-version to run a specific version of GCC.运行 GCC 的常用方法是运行名为gcc的可执行文件,或在交叉编译时运行 machine machine-gcc gcc,或运行特定版本的 GCC 的machine-gcc-version When you compile C++ programs, you should invoke GCC as g++ instead.当你编译 C++ 程序时,你应该把 GCC 作为g++来调用。


Side Note: In the case of the Xcode.app embedded toolchain, g++ simply links to gcc .旁注:在 Xcode.app 嵌入式工具链的情况下, g++只是链接到gcc Thus, g++ invocations may vary on a per-toolchain basis.因此, g++调用可能因每个工具链而异。

ls -l /Applications/Xcode.app/Contents/Developer/usr/bin
# …
# lrwxr-xr-x  1 root  wheel         3 Apr 27  2021 g++ -> gcc
# -rwxr-xr-x  1 root  wheel    167120 Nov 23 20:51 gcc

### -- versus --
which -a g++
# /usr/bin/g++
ls -l /usr/bin/g++
# -rwxr-xr-x  1 root  wheel  137616 Jan  1  2020 /usr/bin/g++

One notable difference is that if you pass a .c file to gcc it will compile as C.一个显着的区别是,如果您将.c文件传递​​给 gcc,它将编译为 C。

The default behavior of g++ is to treat .c files as C++ (unless -xc is specified). g++ 的默认行为是将.c文件视为 C++(除非指定-xc )。

Although the gcc and g++ commands do very similar things, g++ is designed to be the command you'd invoke to compile a C++ program;尽管 gcc 和 g++ 命令做的事情非常相似,但 g++ 被设计为您在编译 C++ 程序时调用的命令。 it's intended to automatically do the right thing.它旨在自动做正确的事情。

Behind the scenes, they're really the same program.在幕后,它们实际上是同一个程序。 As I understand, both decide whether to compile a program as C or as C++ based on the filename extension.据我了解,两者都根据文件扩展名决定将程序编译为 C 还是 C++。 Both are capable of linking against the C++ standard library, but only g++ does this by default.两者都能够链接到 C++ 标准库,但默认情况下只有 g++ 这样做。 So if you have a program written in C++ that doesn't happen to need to link against the standard library, gcc will happen to do the right thing;因此,如果您有一个用 C++ 编写的程序,并且碰巧不需要链接到标准库,那么 gcc 会碰巧做正确的事情; but then, so would g++.但是,g++ 也会如此。 So there's really no reason not to use g++ for general C++ development.所以真的没有理由不使用 g++ 进行一般的 C++ 开发。

I became interested in the issue and perform some experiments我对这个问题产生了兴趣并进行了一些实验

  1. I found that description here , but it is very short.我在这里找到了该描述,但它很短。

  2. Then I tried to experiment with gcc.exe and g++.exe on my windows machine:然后我尝试在我的 Windows 机器上尝试 gcc.exe 和 g++.exe:

     $ g++ --version | head -n1 g++.exe (gcc-4.6.3 release with patches [build 20121012 by perlmingw.sf.net]) 4.6.3 $ gcc --version | head -n1 gcc.exe (gcc-4.6.3 release with patches [build 20121012 by perlmingw.sf.net]) 4.6.3
  3. I tried to compile c89, c99, and c++1998 simple test files and It's work well for me with correct extensions matching for language我尝试编译 c89、c99 和 c++1998 简单测试文件,并且它对我来说工作得很好,具有与语言匹配的正确扩展名

    gcc -std=c99 test_c99.c gcc -std=c89 test_c89.c g++ -std=c++98 test_cpp.cpp gcc -std=c++98 test_cpp.cpp
  4. But when I try to run "gnu compiler collection" tool in that fashion:但是当我尝试以这种方式运行“gnu 编译器集合”工具时:

     $ gcc -std=c++98 test_cpp.c cc1.exe: warning: command line option '-std=c++98' is valid for C++/ObjC++ but not for C [enabled by default]
  5. But this one still work with no errors但是这个仍然可以正常工作

    $ gcc -x c++ -std=c++98 test_cpp.c
  6. And this also而这也

    $ g++ -std=c++0x test_cpp_11.cpp

ps Test files ps 测试文件

$ cat test_c89.c test_c99.c test_cpp.cpp

// C89 compatible file
int main()
{
    int x[] = {0, 2};
    return sizeof(x);
}

// C99 compatible file
int main()
{
    int x[] = {[1]=2};
    return sizeof(x);
}

// C++1998,2003 compatible file
class X{};
int main()
{
    X x;
    return sizeof(x);
}

// C++11
#include <vector>
enum class Color : int{red,green,blue}; // scoped enum
int main()
{
    std::vector<int> a {1,2,3}; // bracket initialization
    return 0;
}

Findings:发现:

  1. If look at process tree then it seems that gcc, and g++ is backend to other tools, which in my environment are: cc1plus.exe, cc1.exe, collect2.exe, as.exe, ld.exe如果查看进程树,那么 gcc 和 g++ 似乎是其他工具的后端,在我的环境中是: cc1plus.exe、cc1.exe、collect2.exe、as.exe、ld.exe

  2. gcc works fine as metatool for if you have correct extension or set correct -std -x flags.如果您有正确的扩展名或设置正确的 -std -x 标志,gcc 可以作为元工具正常工作。 See this看到这个

“GCC” is a common shorthand term for the GNU Compiler Collection. “GCC”是 GNU 编译器集合的常用缩写词。 This is both the most general name for the compiler, and the name used when the emphasis is on compiling C programs (as the abbreviation formerly stood for “GNU C Compiler”).这既是编译器的最通用名称,也是强调编译 C 程序时使用的名称(以前的缩写代表“GNU C Compiler”)。

When referring to C++ compilation, it is usual to call the compiler “G++”.在提到 C++ 编译时,通常将编译器称为“G++”。 Since there is only one compiler, it is also accurate to call it “GCC” no matter what the language context;由于只有一个编译器,因此无论在何种语言环境下,将其称为“GCC”也是准确的; however, the term “G++” is more useful when the emphasis is on compiling C++ programs.但是,当重点是编译 C++ 程序时,术语“G++”更有用。

You could read more here .你可以在这里阅读更多。

I was testing gcc and g++ in a linux system.我在 linux 系统中测试 gcc 和 g++。 By using MAKEFILE, I can define the compliler used by "GNU make".通过使用 MAKEFILE,我可以定义“GNU make”使用的编译器。 I tested with the so called "dynamic memory" locating feature of "C plus plus" by :我使用“C plus plus”的所谓“动态内存”定位功能进行了测试:

int main(){

int * myptr = new int;
* myptr = 1;
printf("myptr[0] is %i\n",*myptr);
return 0;
}

Only g++ can successfully compile on my computer while gcc will report error我的电脑上只有g++可以编译成功,而gcc会报错

undefined reference to `operator new(unsigned long)'

So my own conclusion is gcc does not fully support "C plus plus".所以我自己的结论是gcc并不完全支持“C plus plus”。 It seems that choosing g++ for C++ source files is a better option.似乎为 C++ 源文件选择 g++ 是一个更好的选择。

gcc and g ++ are both GNU compiler. gcc 和 g++ 都是 GNU 编译器。 They both compile c and c++.他们都编译c和c++。 The difference is for *.c files gcc treats it as ac program, and g++ sees it as ac ++ program.不同之处在于 *.c 文件 gcc 将其视为 ac 程序,而 g++ 将其视为 ac ++ 程序。 *.cpp files are considered to be c ++ programs. *.cpp 文件被认为是 c++ 程序。 c++ is a super set of c and the syntax is more strict, so be careful about the suffix. c++是c的超集,语法比较严格,所以要注意后缀。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM