简体   繁体   English

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

[英]Difference between CC, gcc and g++?

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

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). gcc是GCC(GNU编译器集合)中的GNU C编译器。
  • g++ is the GNU C++ Compiler from the GCC. 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. 在Solaris上,CC通常是Sun C ++编译器的名称。
  • On Solaris, cc is normally the name of the Sun C compiler. 在Solaris上,cc通常是Sun C编译器的名称。
  • On Linux, if it exists, CC is probably a link to g++. 在Linux上,如果存在,CC可能是g ++的链接。
  • On Linux, cc is a link to gcc. 在Linux上,cc是gcc的链接。

However, even on Solaris, it could be that cc is the old BSD-based C compiler from /usr/ucb . 但是,即使在Solaris上,也可能是cc是来自/usr/ucb的旧的基于BSD的C编译器。 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. 实际上,通常没有安装,只有一个存根失败,对试图编译和安装自配置软件的人造成严重破坏。

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). 在HP-UX上,默认的“cc”仍然是一个仅支持K&R的C编译器,允许在必要时重新连接内核,并且不能用于现代软件工作,因为它不支持标准C.您必须使用备用编译器名称('acc'IIRC)。 Similarly, on AIX, the system C compiler goes by names such as 'xlc' or 'xlc32'. 类似地,在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; POSIX试图通过要求程序c89(最初)和后来的c99存在来立法解决这个问题; these are the compilers compatible with the ISO/IEC 9899:1989 and 9899:1999 C standards. 这些是符合ISO / IEC 9899:1989和9899:1999 C标准的编译器。 It is doubtful that POSIX succeeded. 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. C编译器和C ++编译器之间存在很大差异。 The C++ compilers will accept C++ programs and will not compile arbitrary C programs. C ++编译器将接受C ++程序,不会编译任意C程序。 (Although it is possible to write C in a subset that is also understood by C++, many C programs are not valid C++ programs). (虽然可以在C ++也理解的子集中编写C,但许多C程序都不是有效的C ++程序)。 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 ++程序通常可以在给定平台上使用C库; C programs cannot usually use C++ libraries. C程序通常不能使用C ++库。 So, C++ has a larger set of libraries available. 因此,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!). 请注意,如果您使用的是Solaris,则由CC生成的目标代码与g ++生成的目标代码不兼容 - 它们是两个独立的编译器,具有诸如异常处理和名称修改之类的单独约定(并且故意更改名称不同以确保不兼容的目标文件没有链接在一起!)。 This means that if you want to use a library compiled with CC, you must compile your whole program with CC. 这意味着如果要使用使用CC编译的库,则必须使用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. 这也意味着如果你想使用一个用CC编译的库和另一个用g ++编译的库,那你就不走运了。 You have to recompile one of the libraries at least. 您必须至少重新编译其中一个库。

In terms of quality of assembler generated, the GCC (GNU Compiler Collection) does a very good job. 在生成汇编程序的质量方面,GCC(GNU编译器集合)做得非常好。 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. 我相信英特尔编译器有更广泛的优化,但尚未在GCC中复制。 But any such pontifications are hazardous while we do not know what platform you are concerned with. 但是,任何这样的指责都是危险的,而我们不知道你关注的是什么平台。

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. 就语言特性而言,编译器通常都非常接近当前标准(C ++ 98,C ++ 2003,C99),但标准语言与编译器支持的语言之间通常存在细微差别。 The older C89 standard support is essentially the same (and complete) for all C compilers. 对于所有C编译器,旧的C89标准支持基本相同(并且完整)。 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. 还有很多选项(特别是使用GCC)来调整编译器的行为。 The GCC has a variety of extensions that make life simpler if you know you are only targetting that compiler family. 如果您知道自己只是针对该编译器系列,那么GCC具有各种扩展,可以简化生活。

CC is an environment variable referring to the system's C compiler. CC是一个环境变量,指的是系统的C编译器。 What it points to (libraries accessible, etc) depend on platform. 它所指向的(库可访问等)取决于平台。 Often it will point to /usr/bin/cc , the actual c complier (driver). 通常它会指向/usr/bin/cc ,实际的c complier(驱动程序)。 On linux platforms, CC almost always points to /usr/bin/gcc . 在Linux平台上, CC几乎总是指向/usr/bin/gcc

gcc is the driver binary for the GNU compiler collection. gcc是GNU编译器集合的驱动程序二进制文件。 It can compile C, C++, and possibly other languages; 它可以编译C,C ++和其他可能的语言; it determines the language by the file extension. 它通过文件扩展名确定语言。

g++ is a driver binary like gcc , but with a few special options set for compiling C++. g++是像gcc一样的驱动程序二进制文件,但是为编译C ++设置了一些特殊选项。 Notably (in my experience), g++ will link libstdc++ by default, while gcc won't. 值得注意的是(根据我的经验), g++默认链接libstdc ++,而gcc则不会。

I wanna add just one information what cc in Linux. 我想在Linux中只添加一个cc的信息。 It is linked with gcc. 它与gcc相关联。 To check it. 检查一下。 在此输入图像描述

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++

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

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