https://github.com/llvm/llvm-project
github下载速度太慢,可以用国内码云(gitee)中转
git clone https://gitee.com/wangwenchaonj/llvm-project.git
sudo apt install gcc
sudo apt install g++
sudo apt install make
sudo apt install cmake
使用目前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
,增大操作系统的内存后,继续增量编译。
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
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++