CC、gcc 和 g++ 之间的区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1516609/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 20:14:30  来源:igfitidea点击:

Difference between CC, gcc and g++?

c++cgcccompilation

提问by vehomzzz

What are the difference between the 3 compilers CC, gcc, g++ when compiling C and C++ code in terms of assembly code generation, available libraries, language features, etc.?

编译C和C++代码时,3个编译器CC、gcc、g++在汇编代码生成、可用库、语言特性等方面有什么区别?

回答by Jonathan Leffler

The answer to this is platform-specific; what happens on Linux is different from what happens on Solaris, for example.

对此的答案是特定于平台的;例如,在 Linux 上发生的事情与在 Solaris 上发生的事情不同。

The easy part (because it is not platform-specific) is the separation of 'gcc' and 'g++':

简单的部分(因为它不是特定于平台的)是 'gcc' 和 'g++' 的分离:

  • gcc is the GNU C Compiler from the GCC (GNU Compiler Collection).
  • g++ is the GNU C++ Compiler from the GCC.
  • gcc 是来自 GCC(GNU 编译器集合)的 GNU C 编译器。
  • g++ 是来自 GCC 的 GNU C++ 编译器。

The hard part, because it is platform-specific, is the meaning of 'CC' (and 'cc').

困难的部分,因为它是特定于平台的,是'CC'(和'cc')的含义。

  • On Solaris, CC is normally the name of the Sun C++ compiler.
  • On Solaris, cc is normally the name of the Sun C compiler.
  • On Linux, if it exists, CC is probably a link to g++.
  • On Linux, cc is a link to gcc.
  • 在 Solaris 上,CC 通常是 Sun C++ 编译器的名称。
  • 在 Solaris 上,cc 通常是 Sun C 编译器的名称。
  • 在 Linux 上,如果存在,CC 可能是 g++ 的链接。
  • 在 Linux 上,cc 是 gcc 的链接。

However, even on Solaris, it could be that cc is the old BSD-based C compiler from /usr/ucb. In practice, that usually isn't installed and there's just a stub that fails, wreaking havoc on those who try to compile and install self-configuring software.

但是,即使在 Solaris 上,cc 也可能是来自/usr/ucb. 在实践中,通常没有安装它,只有一个失败的存根,对那些试图编译和安装自配置软件的人造成严重破坏。

On HP-UX, the default 'cc' is still a K&R-only C compiler installed to permit relinking of the kernel when necessary, and unusable for modern software work because it doesn't support standard C. You have to use alternative compiler names ('acc' IIRC). Similarly, on AIX, the system C compiler goes by names such as 'xlc' or 'xlc32'.

在 HP-UX 上,默认的“cc”仍然是安装的仅支持 K&R 的 C 编译器,以允许在必要时重新链接内核,并且无法用于现代软件工作,因为它不支持标准 C。您必须使用其他编译器名称('acc' IIRC)。类似地,在 AIX 上,系统 C 编译器使用诸如“xlc”或“xlc32”之类的名称。

Classically, the default system compiler was called 'cc' and self-configuring software falls back on that name when it doesn't know what else to use.

传统上,默认系统编译器被称为“cc”,当自配置软件不知道还能使用什么时,它会使用该名称。

POSIX attempted to legislate its way around this by requiring the programs c89 (originally) and later c99 to exist; these are the compilers compatible with the ISO/IEC 9899:1989 and 9899:1999 C standards. It is doubtful that POSIX succeeded.

POSIX 试图通过要求程序 c89(最初)和后来的 c99 存在来立法解决这个问题。这些是与 ISO/IEC 9899:1989 和 9899:1999 C 标准兼容的编译器。POSIX 是否成功值得怀疑。



The question asks about the differences in terms of features and libraries. As before, the answer is platform specific in part, and generic in part.

该问题询问功能和库方面的差异。和以前一样,答案部分是特定于平台的,部分是通用的。

The big divide is between the C compilers and the C++ compilers. The C++ compilers will accept C++ programs and will not compile arbitrary C programs. (Although it is possible to write C in a subset that is also understood by C++, many C programs are not valid C++ programs). Similarly, the C compilers will accept C programs and will reject most C++ programs (because most C++ programs use constructs not available in C).

C 编译器和 C++ 编译器之间存在很大的分歧。C++ 编译器将接受 C++ 程序并且不会编译任意的 C 程序。(尽管可以在 C++ 也能理解的子集中编写 C,但许多 C 程序不是有效的 C++ 程序)。类似地,C 编译器将接受 C 程序并拒绝大多数 C++ 程序(因为大多数 C++ 程序使用 C 中不可用的构造)。

The set of libraries available for use depends on the language. C++ programs can usually use C libraries on a given platform; C programs cannot usually use C++ libraries. So, C++ has a larger set of libraries available.

可供使用的库集取决于语言。C++ 程序通常可以在给定平台上使用 C 库;C 程序通常不能使用 C++ 库。因此,C++ 有更多可用的库。

Note that if you are on Solaris, the object code produced by CC is not compatible with the object code produced by g++ -- they are two separate compilers with separate conventions for things such as exception handling and name mangling (and the name mangling is deliberately different to ensure that incompatible object files are not linked together!). This means that if you want to use a library compiled with CC, you must compile your whole program with CC. It also means that if you want to use one library compiled with CC and another compiled with g++, you are out of luck. You have to recompile one of the libraries at least.

请注意,如果您在 Solaris 上,则 CC 生成的目标代码与 g++ 生成的目标代码不兼容——它们是两个独立的编译器,对于诸如异常处理和名称修改之类的事情有不同的约定(并且名称修改是故意的以确保不兼容的目标文件不会链接在一起!)。这意味着如果你想使用一个用 CC 编译的库,你必须用 CC 编译你的整个程序。这也意味着,如果你想使用一个用 CC 编译的库和另一个用 g++ 编译的库,那你就不走运了。您必须至少重新编译其中一个库。

In terms of quality of assembler generated, the GCC (GNU Compiler Collection) does a very good job. But sometimes the native compilers work a bit better. The Intel compilers have more extensive optimizations that have not yet been replicated in GCC, I believe. But any such pontifications are hazardous while we do not know what platform you are concerned with.

在生成的汇编器质量方面,GCC(GNU Compiler Collection)做得非常好。但有时本机编译器工作得更好一些。我相信,英特尔编译器具有更广泛的优化,这些优化尚未在 GCC 中复制。但是,任何此类夸夸其谈都是危险的,而我们不知道您关注的是哪个平台。

In terms of language features, the compilers all generally hew fairly close to the current standards (C++98, C++2003, C99), but there are usually small differences between the standard language and the language supported by the compiler. The older C89 standard support is essentially the same (and complete) for all C compilers. There are differences in the darker corners of the language. You need to understand 'undefined behaviour', 'system defined behaviour' and 'unspecified behaviour'; if you invoke undefined behaviour, you will get different results at different times. There are also many options (especially with the GCC) to tweak the behaviour of the compiler. The GCC has a variety of extensions that make life simpler if you know you are only targetting that compiler family.

在语言特性方面,编译器一般都比较接近当前的标准(C++98、C++2003、C99),但标准语言和编译器支持的语言之间通常存在细微差别。对于所有 C 编译器,旧的 C89 标准支持基本上是相同的(并且是完整的)。语言的阴暗角落存在差异。您需要了解“未定义行为”、“系统定义行为”和“未指定行为”;如果您调用未定义的行为,您将在不同的时间得到不同的结果。还有很多选项(尤其是 GCC)来调整编译器的行为。如果您知道自己只针对该编译器系列,则 GCC 具有多种扩展功能,可以让您的工作变得更简单。

回答by Managu

CCis an environment variable referring to the system's C compiler. What it points to (libraries accessible, etc) depend on platform. Often it will point to /usr/bin/cc, the actual c complier (driver). On linux platforms, CCalmost always points to /usr/bin/gcc.

CC是指系统的 C 编译器的环境变量。它指向的内容(可访问的库等)取决于平台。通常它会指向/usr/bin/cc实际的 c 编译器(驱动程序)。在 linux 平台上,CC几乎总是指向/usr/bin/gcc.

gccis the driver binary for the GNU compiler collection. It can compile C, C++, and possibly other languages; it determines the language by the file extension.

gcc是 GNU 编译器集合的驱动程序二进制文件。它可以编译 C、C++ 和其他可能的语言;它通过文件扩展名确定语言。

g++is a driver binary like gcc, but with a few special options set for compiling C++. Notably (in my experience), g++will link libstdc++ by default, while gccwon't.

g++是一个类似于 的驱动程序二进制文件gcc,但设置了一些用于编译 C++ 的特殊选项。值得注意的是(根据我的经验),g++默认情况下会链接 libstdc++,而gcc不会。

回答by Uddhav Gautam

I wanna add just one information what cc in Linux. It is linked with gcc. To check it. enter image description here

我只想添加一个信息 what cc in Linux。它与 gcc 相关联。要检查它。 在此处输入图片说明

Similarly, the same thing with c++.

同样,与 c++ 相同。

uddhavpgautam@UbuntuServer1604:~/Desktop/c++$ whereis c++
c++: /usr/bin/c++ /usr/include/c++ /usr/share/man/man1/c++.1.gz  
uddhavpgautam@UbuntuServer1604:~/Desktop/c++$ ls -l /usr/bin/c++
lrwxrwxrwx 1 root root 21 Jul 31 14:00 /usr/bin/c++ -> /etc/alternatives/c++
uddhavpgautam@UbuntuServer1604:~/Desktop/c++$ ls -l /etc/alternatives/c++
lrwxrwxrwx 1 root root 12 Jul 31 14:00 /etc/alternatives/c++ -> /usr/bin/g++