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 
#include 

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

你可能感兴趣的:(clang,c++)