centos7编译安装llvm

写在前面

       LLVM是构架编译器(compiler)的框架系统,以C++编写而成,用于优化以任意程序语言编写的程序的编译时间(compile-time)、链接时间(link-time)、运行时间(run-time)以及空闲时间(idle-time),对开发者保持开放,并兼容已有脚本。

       LLVM计划启动于2000年,最初由美国UIUC大学的Chris Lattner博士主持开展。2006年Chris Lattner加盟Apple Inc.并致力于LLVM在Apple开发体系中的应用。Apple也是LLVM计划的主要资助者。

       目前LLVM已经被苹果IOS开发工具、Xilinx Vivado、Facebook、Google等各大公司采用。
前面一片博客已经介绍过编译安装cmake,在这里就要使用cmake来安装llvm。

1. 下载llvm源码

我下载的是8.0.0的代码包,需要下载4个文件:

LLVM source code

Clang source code

Clang Tools Extra source code

Compiler RT source code

如果服务器可以联网的话,可以使用下面命令直接下载。

mkdir llvm-8.0.0
cd llvm-8.0.0
wget http://releases.llvm.org/8.0.0/llvm-8.0.0.src.tar.xz
wget http://releases.llvm.org/8.0.0/cfe-8.0.0.src.tar.xz
wget http://releases.llvm.org/8.0.0/clang-tools-extra-8.0.0.src.tar.xz
wget http://releases.llvm.org/8.0.0/compiler-rt-8.0.0.src.tar.xz

2. 解压

下载好了以后,四个压缩包都解压出来,

tar -xJvf llvm-8.0.0.src.tar.xz
tar -xJvf cfe-8.0.0.src.tar.xz
tar -xJvf clang-tools-extra-8.0.0.src.tar.xz
tar -xJvf compiler-rt-8.0.0.src.tar.xz

得到四个目录:
llvm-8.0.0.src

cfe-8.0.0.src

clang-tools-extra-8.0.0.src

compiler-rt-8.0.0.src

然后按下面的步骤组织:

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

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

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

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

3. 配置并编译安装

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

mkdir build-8.0.0
cd build-8.0.0

这样做方便出错的时候删除build-8.0.0中的内容重新编译,不污染源码。
然后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-8.0.0.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).

注意 : 这后两个选项推荐大家加上 ,否则会产生巨大的文件.

然后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- 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

4. 后续工作

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

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

测试使用

在终端输入

clang --version

       显示下面的结果,则为安装成功。
clang --version

文中都是我个人的理解,如有错误的地方欢迎下方评论告诉我,我及时更正,大家共同进步

  • 5
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值