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

Difference between CC, gcc and g++?(CC、gcc 和 g++ 之间的区别?)

本文介绍了CC、gcc 和 g++ 之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.?

推荐答案

这个问题的答案是特定于平台的;例如,Linux 上发生的事情与 Solaris 上发生的事情不同.

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

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

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

  • gcc 是 GCC(GNU 编译器集合)中的 GNU C 编译器.
  • g++ 是来自 GCC 的 GNU C++ 编译器.

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

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

  • 在 Solaris 上,CC 通常是 Sun C++ 编译器的名称.
  • 在 Solaris 上,cc 通常是 Sun C 编译器的名称.
  • 在 Linux 上,如果存在,CC 可能是 g++ 的链接.
  • 在 Linux 上,cc 是 gcc 的链接.

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

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.

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

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'.

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

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.

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

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.

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

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.

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

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++ 有更多可用的库.

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.

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

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.

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

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.

在语言特性方面,编译器一般都与当前的标准(C++98、C++2003、C99)相当接近,但标准语言和支持的语言之间通常存在细微差别.编译器.对于所有 C 编译器,旧的 C89 标准支持基本上是相同的(并且是完整的).语言的阴暗角落存在差异.您需要了解未定义行为"、系统定义行为"和未指定行为";如果您调用未定义的行为,您将在不同的时间得到不同的结果.还有许多选项(尤其是使用 GCC)来调整编译器的行为.如果您知道自己只针对该编译器系列,则 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.

这篇关于CC、gcc 和 g++ 之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:CC、gcc 和 g++ 之间的区别?

基础教程推荐