linux下c/c++ 编译器:gcc/g++ vs clang/clang++

以前一直使用gcc/g++,从知乎发现了关于clang的讨论,发现clang的错误提示比gcc好多了,还能兼容g++ 和 gcc,g++/gcc的一些命令在clang上也继承了,迁移姿势不费力:-)。


目录


clang项目目标

The goal of the Clang project is to create a new C based language
front-end: C, C++, Objective C/C++, OpenCL C and others for the LLVM
compiler.

clang项目开发原因

Development of the new front-end was started out of a need for a
compiler that allows better diagnostics, better integration with IDEs,
a license that is compatible with commercial products, and a nimble
compiler that is easy to develop and maintain. All of these were
motivations for starting work on a new front-end that could meet these needs.

clang一些特性

  • 编译速度比gcc快
  • 占用内存比gcc少
  • 模块化设计,便于IDE集成

clang下载

下载llvm 和clang

$ sudo apt-get install llvm
$ sudo apt-get install clang

clang设置

切换默认c++编译器:

$ sudo update-alternatives --config c++

切换默认c编译器:

$ sudo update-alternatives --config cc

clang使用

编译c程序

$ clang -Wall -g xxx.c -o xxx

编译c++程序

$ clang++ -Wall -g -std=c++11 -stdlib = libstdc++ xxx.cpp -o xxx

std参数指的是编译的标准,可选为:c++98/c++03/c++11/c++0x
有关clang更多参数请看这里或者使用命令"clang --help"查看.

参考资料:

结构化编译器前端 Clang 介绍
LLVM 与 Clang 介绍
在 Linux 下使用 clang 3.0 + llvm 3.0 + libc++

打赏作者