Clang 比 GCC 编译器的优势:

编译速度更快

编译产出更小

出错提示更友 好,比如 clang 在编译过程可以直接指出相对简单的出错位置以及它 “ 认为 ” 正确的方式 。

内置有静态分析工具,可以对代码进行静态分析 (clang—analyze) 。这也是 gcc 做不到的 。

专注,因为 clang 只需要完成词法和语法分析,代码优化和机器代码的生成工作由 llvm 完成。所以和全部由自己包下的 gcc 比起来, clang 可以更专注地做好一件事。这种结构也使 clang 可以被单独拿出来用在其他的程序里,成为其它 app (主要是 IDE)的内嵌 C/C++ parser 。 对于 IDE 而言,代码补全、重构是重要的功能,然而如果没有底层的支持,只使用 tags 分析或是正则表达式匹配是很难达成的, clang正好充当了这一角色。 这样, editor 工具可以使用和 compiler 一样的 parser 来完成 edit-time 的语法检查 。 而 gcc 就没法很方便地做到这一点 。由于历史原因, GCC 是一个单一的可执行程序编译器,其内部完成了从预处理到最后代码生成的全部过程,中间诸多信息都无法被其他程序重用。

 Gcc 的优势:

·         一些软件用 clang 编译会出现莫名其妙的错误,但是用 gcc 编译可以通过 。

·         GCC 在 5.0 之前一直都在准备用 C++ 实现模块化,期待 GCC 的 5.0 会有所突破,补上无法模块化的短板。
 

 
编译速度更快、编译产出更小、出错提示更友好。尤其是在比较极端的情况下。

两年多前曾经写过一个Scheme解释器,词法分析和语法解析部分大约2000行,用的是Boost.Spirit——一个重度依赖C++模版元编程的框架。当时用g++ 4.2编译的情况是:

  1. 编译速度极慢:完整编译一次需要20分钟
  2. 编译过程中内存消耗极大:单个g++实例内存峰值消耗超过1G
  3. 中间产出物极大:编译出的所有.o文件加在一起大约1~2G,debug链接产物超过200M
  4. 编译错误极其难以理解:编译错误经常长达几十K,基本不可读,最要命的是编译错误经常会长到被g++截断,看不到真正出错的位置,基本上只能靠裸看代码来调试

这里先不论我使用Spirit的方式是不是有问题,或者Spirit框架自身的问题。我当时因为实在忍受不了g++,转而尝试clang。当时用的是clang 2.8,刚刚可以完整编译Boost,效果让我很满意:

  1. 编译速度有显著提升,记得大约是g++的1/3或1/4
  2. 编译过程中的内存消耗差别好像不大
  3. 中间产出物及最终链接产物,记得也是g++的1/3或1/4
  4. 相较于g++,编译错误可读性有所飞跃,至少不会出现编译错误过长被截断的问题了

当时最大的缺点是clang编译出的可执行文件无法用gdb调试,需要用调试器的时候还得用g++再编译一遍。不过这个问题后来解决了,我不知道是clang支持了gdb还是gdb支持了clang。至少我当前在Ubuntu下用clang 3.0编译出的二进制文件已经可以顺利用gdb调试了。

最后一点,其他同学也有讲到,就是Clang采用的是BSD协议。这是苹果资助LLVM、FreeBSD淘汰GCC换用Clang的一个重要原因。
 

 

Clang vs GCC (GNU Compiler Collection)

Pro's of GCC vs clang:

  • GCC supports languages that clang does not aim to, such as Java, Ada, FORTRAN, Go, etc.
  • GCC supports more targets than LLVM.
  • 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 clang vs GCC:

  • 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 a compiler works. GCC has a very old codebase which presents a steep learning curve to new developers.
  • 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 of the compiler.
  • 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.
  • 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'.
  • 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).
  • Clang is much faster and uses far less memory than GCC.
  • 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.
  • 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.
  • 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.
  • Clang's support for C++ is more compliant than GCC's in many ways.
  • 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.

linux下Clang和gcc的区别的更多相关文章

  1. 在Linux下如何使用GCC编译程序、简单生成 静态库及动态库

      最近在编写的一个Apache  kafka 的C/C++客户端,,在看他写的 example中,他的编译是用librdkafka++.a和librdkafka.a    静态库编译的,,,而我们这 ...

  2. linux 下多版本gcc 共存问题

    linux 下多版本gcc 共存问题 http://blog.csdn.net/isfirst/article/details/42296583 参考 http://blog.csdn.net/chi ...

  3. [转] linux 下 进程和线程的区别

    1.进程与线程 进程是程序执行时的一个实例,即它是程序已经执行到课中程度的数据结构的汇集.从内核的观点看,进程的目的就是担当分配系统资源(CPU时间.内存等)的基本单位. 线程是进程的一个执行流,是C ...

  4. Windows下与Linux下编写socket程序的区别 《转载》

     原文网址:http://blog.chinaunix.net/uid-2270658-id-308160.html [[Windows]] [Windows: 头文件的区别] #include< ...

  5. [进程管理]linux 下 进程和线程的区别(baidu 面试)

    进程是程序执行时的一个实例,即它是程序已经执行到课中程度的数据结构的汇集.从内核的观点看,进程的目的就是担当分配系统资源(CPU时间.内存等)的基本单位. 线程是进程的一个执行流,是CPU调度和分派的 ...

  6. linux 下 进程和线程的区别

    1.进程与线程 进程是程序执行时的一个实例,即它是程序已经执行到课中程度的数据结构的汇集.从内核的观点看,进程的目的就是担当分配系统资源(CPU时间.内存等)的基本单位. 线程是进程的一个执行流,是C ...

  7. linux下 su 与 su - 的区别和使用

    Linux下su与su -命令的区别   在启动服务器ntpd服务时遇到一个问题 使用 su root 切换到root用户后,不可以使用service命令: 使用 su - 后,就可以使用servic ...

  8. Linux下tmpfs与ramfs的区别

      ramfs是Linux下一种基于RAM做存储的文件系统.在使用过程中你就可以把ramfs理解为在普通的HDD上建立了一个文件系统,而现在HDD被替换成了RAM,因为是RAM做存储所以会有很高的存储 ...

  9. docker在windows下和linux下网络底层的一些区别

    windows和linux下的docker运行时的网络结构是有区别的 a.windows下,默认使用Hyper-v创建一个linux虚拟机,承载docker.所以从外向内的路径为: windows - ...

随机推荐

  1. netstat 的10个基本用法(转)

    本文转载自一译作. *注:netstat即network state缩写. Netstat 简介 Netstat 是一款命令行工具,可用于列出系统上所有的网络套接字连接情况,包括 tcp, udp 以 ...

  2. android 开发从入门到精通

    Android-Tips This is an awesome list of tips for android. If you are a beginner, this list will be t ...

  3. OpenCV——色调映射

    // define head function #ifndef PS_ALGORITHM_H_INCLUDED #define PS_ALGORITHM_H_INCLUDED #include < ...

  4. mahout系列之---谱聚类

    1.构造亲和矩阵W 2.构造度矩阵D 3.拉普拉斯矩阵L 4.计算L矩阵的第二小特征值(谱)对应的特征向量Fiedler 向量 5.以Fiedler向量作为kmean聚类的初始中心,用kmeans聚类 ...

  5. android studio设置代理更新

    我们都知道Android Studio是基于IDEA开发的,而我们写的每一个程序又都是有Gradle构建的,Gradle的优点可以说是很多,被很多程序员夸得没边,但是它有一个特点还是值得我们注意的.我 ...

  6. ruby和linux shell共同编程的示例

    有了shell为毛还要ruby呢?话不能这么说,有些小功能用ruby还是很方便的,比如说字符串的反转再加1功能用shell来写就比较麻烦.str="123456",我们定义一个反转 ...

  7. JasperReport的安装

    下载网址http://community.jaspersoft.com/community-download

  8. Symmetric Tree 对称树

    判断一棵二叉树是否为对称的树.如 1 / \ 2 2 / \ / \ 3 4 4 3 观察上面的树可以看出:左子树的右子树等于右子树的左子树,左子树的左子树等于右子树的右子树. 首先可以使用递归.递归 ...

  9. OO开发思想:面向对象的开发方法(Object oriented,OO)

    面向对象的开发方法(Object oriented,OO)认为是好文章吧,拿来分享一下(转载) 面向对象的开发方法(Object oriented,OO) 从事软件开发的工程 师们常常有这样 的体会: ...

  10. Java学习不走弯路教程(7.Eclipse环境搭建)

    7.Eclipse环境搭建 在前几章,我们熟悉了DOS环境下编译和运行Java程序,对于大规模的程序编写,开发工具是必不可少的.Java的开发工具比较常用的是Eclipse.在接下来的教程中,我们将基 ...