CentOS 7 编译安装clang+llvm

天在CentOS上将最新版本的LLVM套件(LLVM,Clang和Libc++)编译安装好了,中间遇到了不少问题。虽然已经有很多博客介绍如何编译安装LLVM了,但是根据我自己亲身体验的情况来看,还是有地方需要注意的,也有东西值得跟大家分享一下。

下面开始:

首先从http://releases.llvm.org/download.html#4.0.1这边下载最新的版本。如果从SVN下载代码的话,已经是5.0的开发版本了。如果需要使用稳定版本,推荐自己下载4.0.1的代码包,需要下载4个文件:

LLVM source code

Clang source code

Clang Tools Extra source code

Compiler RT source code

下载好了以后,四个压缩包都解压出来,得到四个目录:

llvm-4.0.1.src

cfe-4.0.1.src

clang-tools-extra-4.0.1.src

compiler-rt-4.0.1.src

然后按下面的步骤组织:

mv cfe-4.0.1.src clang
mv clang/ llvm-4.0.1.src/tools/

mv clang-tools-extra-4.0.1.src extra
mv extra/ llvm-4.0.1.src/tools/clang/

mv compiler-rt-4.0.1.src compiler-rt
mv compiler-rt llvm-4.0.1.src/projects/

这样以后clang,clang-tools-extra和compiler-rt就可以和llvm一起编译了。

在llvm-4.0.1.src同一层目录上新建个目录build-4.0.1并进入:

mkdir build-4.0.1
cd build-4.0.1

然后Configure and build LLVM and Clang:

cmake -G "Unix Makefiles" -DCMAKE_INSTALL_PREFIX=/usr/local/clang -DCMAKE_BUILD_TYPE=Release -DLLVM_ENABLE_ASSERTIONS=On ../llvm-4.0.1.src/
  • -DCMAKE_INSTALL_PREFIX=directory — Specify for directory the full pathname of where you want the LLVM tools and libraries to be installed (default /usr/local).
  • -DCMAKE_BUILD_TYPE=type — Valid options for type are Debug, Release, RelWithDebInfo, and MinSizeRel. Default is Debug.
  • -DLLVM_ENABLE_ASSERTIONS=On — Compile with assertion checks enabled (default is Yes for Debug builds, No for all other build types).

注意 : 这后两个选项推荐大家加上 ,否则会产生巨大的文件,我第一次编译的时候,没有加上这两个选项,编译出来的结果多大 9.4G (够吓人的吧?把我的磁盘都耗尽了,还报错,google了好久才发现是磁盘空间不够) ,耗时超过一个半小时。

然后make:

make -j 4
  • The default target (i.e. make) will build all of LLVM
  • The check-all target (i.e. make check-all) will run the regression tests to ensure everything is in working order.
  • CMake will generate build targets for each tool and library, and most LLVM sub-projects generate their own check-<project> target.
  • Running a serial build will be slow. Make sure you run a parallel build; for make, use make -j.

尽量把几个核都用上,加快编译速度。

然后make install:

sudo make install

最后,因为是按照自己指定的路径安装,所以需要添加环境变量。

echo "export PATH=$PATH:/usr/local/clang/bin" >> /etc/bashrc
. /etc/bashrc 

安装过程主要参考这几篇文章:

http://clang.llvm.org/get_started.html

http://llvm.org/docs/GettingStarted.html#checkout

http://www.cnblogs.com/Frandy/archive/2012/10/20/llvm_clang_libcxx_cxx11.html

http://www.cnblogs.com/ralphjzhang/archive/2011/12/02/2272671.html

http://www.tuicool.com/articles/ZBveeu

原文地址:https://www.cnblogs.com/BinBinStory/p/7499527.html