Similarities and differences between GCC and Clang

GCC: GNU (Gnu's Not Unix) Compiler Collection (GNU Compiler Collection, GCC), refers to a set of programming language compilers, free software issued under the GPL and LGPL licenses , and is also a key part of the GNU project and the GNU tool chain One of the main components. GCC (especially the C language compiler) is often regarded as the de facto standard for cross-platform compilers. It was developed by Richard Matthew Stallman in 1985 and is now maintained by the Free Software Foundation. GCC was originally developed in C. Later, due to the rise of LLVM and Clang, it converted the development language to C++ more quickly.

Languages ​​supported by GCC : Originally named GNU C Compiler (GNU C Compiler), because it can only handle C language originally. GCC was quickly expanded after its release and became able to handle C++. Later, it can also handle Fortran, Pascal, Objective-C, Java, Ada, Go and other languages.

Many operating systems, including many Unix-like systems, such as Linux and BSD families, use GCC as a standard compiler. The Mac OS X operating system pre-installed on Apple computers also uses this compiler.

GCC is currently maintained by several groups of programmers around the world. It is the compiler that is ported to the most central processor architectures and most operating systems. Since GCC has become the official compiler of the GNU system (including the GNU/Linux family), it has also become the main compiler for compiling and creating other operating systems, including the BSD family, Mac OS X, NeXTSTEP and BeOS.

GCC is usually the compiler of choice for cross-platform software. Unlike compilers that are generally limited to specific systems and operating environments, GCC uses the same front-end processing program on all platforms to generate the same intermediary code. Therefore, this intermediary code is compiled with GCC on various other platforms. The chance to get the correct output program.

The main processor architectures supported by GCC : ARM, x86, x86-64, MIPS, PowerPC, etc.

        GCC structure : The external interface of GCC looks like a standard Unix compiler. The user enters the gcc program name and some command parameters on the command line to determine the individual language compiler used by each input file, and uses the assembly language compiler suitable for this hardware platform for the output code, and selectively runs the link To make executable programs. Each language compiler is an independent program, which can process the input source code and output assembly language code. All language compilers have a common intermediary architecture: a front end parses the source code matching this language and generates an abstract syntax tree, and a translation of this syntax tree becomes the back end of GCC's register conversion language. Compiler optimization and static code analysis techniques are applied to the code at this stage. Finally, the assembly language code suitable for this hardware architecture is produced by an algorithm invented by Jack Davidson and Chris Fraser.

Almost all of GCC is written in C/C++, except for the Ada front end, most of which are written in Ada.

        Clang: is a compiler front end for C, C++, Objective-C and Objective-C++ programming languages . It uses the underlying virtual machine (LLVM) as its backend. Its goal is to provide an alternative to the GNU Compiler Suite (GCC). The author is Chris Lattner (Chris Lattner), under the sponsorship of Apple to develop, and the source code authorization is the use of BSD-like open source license of the University of Illinois at Urbana-Champaign. Clang is mainly written in C++.

The Clang project includes Clang front-end and Clang static analyzer. This software project was initiated by Apple Computer in 2005. It is the front-end of the LLVM (Low Level Virtual Machine) compiler tool set. The purpose is to output the abstract syntax tree (Abstract Syntax Tree, AST) corresponding to the code. The code is compiled into LLVM Bitcode. Then use LLVM on the back-end to compile it into platform-specific machine language.

Clang itself has excellent performance, and the memory consumed by the AST generated by it is only about 20% of GCC. The FreeBSD 10.0 version released in January 2014 uses Clang/LLVM as the default compiler.

        Clang performance : The test proves that Clang compiles Objective-C code at 3 times the speed of GCC, and it can also give accurate suggestions for the compilation errors of users.

        The difference between GCC and Clang :

        GCC features : In addition to supporting C/C++/Objective-C/Objective-C++ languages, it still supports Java/Ada/Fortran/Go, etc.; the current Clang C++ support lags behind GCC; it supports more platforms; it is more popular and widely used , Support is complete.

        Clang features : fast compilation speed; small memory footprint; compatible with GCC; clear and simple design, easy to understand, easy to expand and enhance; modular design based on library, easy to IDE integration; more friendly error prompts.

        The license used by Clang is BSD, and GCC is GPLv3 .

        They use different macros :

        (1) The macros defined by GCC include:

 

__GNUC__
__GNUC_MINOR__
__GNUC_PATCHLEVEL__
__GNUG__


 (2) In addition to supporting macros defined by GCC, Clang also defines:

__clang__
__clang_major__
__clang_minor__
__clang_patchlevel__

 ​​​​​​​

Clang vs GCC(GNU Compiler Collection):

Pro's of GCC vs clang:

(1)、GCC supports languages that clang does not aim to, such as Java, Ada, FORTRAN, Go, etc.

(2)、GCC supports more targets than LLVM.

(3)、GCC supports many language extensions, some of which are not implemented by Clang. For instance, in C mode, GCC supports nested functions and has an extension allowing VLAs in structs.

Pro's of clangvs GCC:

(1)、The Clang ASTs and design are intended to be easily understandable by anyone who is familiar with the languages involved and who has a basic understanding of how acompiler works. GCC has a very old codebase which presents a steep learning curve to new developers.

(2)、Clang is designed as an API from its inception, allowing it to be reused by source analysis tools, refactoring, IDEs (etc) as well as for code generation. GCC is built as a monolithic static compiler, which makes it extremely difficult to use as an API and integrate into other tools. Further, its historic design and current policy makes it difficult to decouple the front-end from the rest ofthe compiler.

(3)、Various GCC design decisions make it very difficult to reuse: its build system is difficult to modify, you can't link multiple targets into one binary, you can't link multiple front-ends into one binary, it uses a custom garbage collector, uses global variables extensively, is not reentrant or multi-threadable, etc. Clang has none of these problems.

(4)、Clang does not implicitly simplify code as it parses it like GCC does. Doing so causes many problems for source analysis tools: as one simple example, if you write"x-x" in your source code, the GCC AST will contain "0",with no mention of 'x'. This is extremely bad for a refactoring tool that wants to rename 'x'.

(5)、Clang can serialize its AST out to disk and read it back into another program, which is useful for whole program analysis. GCC does not have this. GCC's PCH mechanism(which is just a dump of the compiler memory image) is related, but is architecturally only able to read the dump back into the exact same executable as the one that produced it (it is not a structured format).

(6)、Clang is much faster and uses far less memory than GCC.

(7)、Clang has been designed from the start to provide extremely clear and concise diagnostics(error and warning messages), and includes support for expressive diagnostics.Modern versions of GCC have made significant advances in this area,incorporating various Clang features such as preserving typedefs in diagnostics and showing macro expansions, but GCC is still catching up.

(8)、GCC is licensed under the GPL license. clang uses a BSD license, which allows it to be embedded in software that is not GPL-licensed.

(9)、Clang inherits a number of features from its use of LLVM as a backend, including support for a bytecode representation for intermediate code, pluggable optimizers, link-time optimization support, Just-In-Time compilation, ability to link in multiple code generators, etc.

(10)、Clang's support for C++ is more compliant than GCC's in many ways.

(11)、Clang supports many language extensions, some of which are not implemented by GCC. For instance, Clang provides attributes for checking thread safety and extended vector types.

 

Guess you like

Origin blog.csdn.net/wu347771769/article/details/82775665