ubuntu18.04编译安装clang/llvm

1 下载clang/llvm源码

https://github.com/llvm/llvm-project
github下载速度太慢,可以用国内码云(gitee)中转

git clone https://gitee.com/wangwenchaonj/llvm-project.git

2 更新编译工具链

sudo apt install gcc
sudo apt install g++
sudo apt install make
sudo apt install cmake 

3 编译clang/llvm

使用目前clang/llvm最新的稳定发布版本分支进行编译:

git clone https://gitee.com/wangwenchaonj/llvm-project.git
cd llvm-project
git checkout release/10.x
mkdir build
cd build

cmake -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=Release --enable-optimized --enable-targets=host-only  ../llvm -DLLVM_ENABLE_PROJECTS="clang;libcxx;libcxxabi;compiler-rt;clang-tools-extra;openmp;lldb;lld" 

make -j2
make install

-DCMAKE_BUILD_TYPE=type — Valid options for type are Debug, Release, RelWithDebInfo, and MinSizeRel. Default is Debug.

-DLLVM_ENABLE_PROJECTS='...' — semicolon-separated list of the LLVM sub-projects you’d like to additionally build. Can include any of: clang, clang-tools-extra, libcxx, libcxxabi, libunwind, lldb, compiler-rt, lld, polly, or debuginfo-tests.
For example, to build LLVM, Clang, libcxx, and libcxxabi, use -DLLVM_ENABLE_PROJECTS="clang;libcxx;libcxxabi".

注意:用虚拟机编译可能会有编译错误internal compiler error,增大操作系统的内存后,继续增量编译。

4 clang编译测试

clang --version

main.cc

#include <iostream>
#include <vector>

int main() {
  std::cout << "Hello World!" << std::endl;
  return 0;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.3 FATAL_ERROR)
add_executable(hello main.cc)
mkdir build && cd build CC="clang" CXX="clang++" cmake -G "Unix Makefiles" ../
make
./hello

或者

clang -o hello main.cc
./hello

5 修改编译器为clang/llvm

root@ubuntu:/home/~# which clang
/usr/local/bin/clang
root@ubuntu:/home/~# which clang++
/usr/local/bin/clang++

export CC=/usr/local/bin/clang
export CXX=/usr/local/bin/clang++
export CXXFLAGS=-stdlib=libc++

制定本地安装的gcc编译器

export CC=/usr/local/bin/gcc
export CXX=/usr/local/bin/g++

6 参考链接

  1. https://www.cnblogs.com/whuwzp/p/ubuntu_vim_2.html
  2. https://www.jianshu.com/p/f071f2486bac
  3. https://www.jianshu.com/p/1590ab2bb444
  4. https://releases.llvm.org/download.html#10.0.0
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值