ubuntu18.04编译安装clang/llvm

本文提供在Ubuntu环境下从源码编译Clang/LLVM的详细步骤,包括下载源码、更新工具链、配置编译参数、解决常见错误及测试编译成果。适用于希望深入了解或定制Clang/LLVM的开发者。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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
<think>我们正在寻找clangd15.0.0的Linux预编译包(x86_64架构)。根据LLVM官方发布页面,我们可以找到对应版本的预编译包。注意:用户之前要求的是15.0.3,现在改成了15.0.0。所以我们需要调整版本号。在LLVM的官方GitHubRelease页面,我们可以找到15.0.0版本的发布:https://github.com/llvm/llvm-project/releases/tag/llvmorg-15.0.0在该页面中,我们可以找到预编译包,其中包含clangd。对于Linuxx86_64系统,通常文件名格式为:`clang+llvm-15.0.0-x86_64-linux-gnu-ubuntu-<版本>.tar.xz`根据历史发布,Ubuntu18.04是一个常见的基准。因此,我们可以给出如下链接:###官方下载地址**clangd15.0.0Linux预编译(x86_64架构)**:👉[https://github.com/llvm/llvm-project/releases/download/llvmorg-15.0.0/clang+llvm-15.0.0-x86_64-linux-gnu-ubuntu-18.04.tar.xz](https://github.com/llvm/llvm-project/releases/download/llvmorg-15.0.0/clang+llvm-15.0.0-x86_64-linux-gnu-ubuntu-18.04.tar.xz)###安装步骤```bash#下载并解压到目标目录wgethttps://github.com/llvm/llvm-project/releases/download/llvmorg-15.0.0/clang+llvm-15.0.0-x86_64-linux-gnu-ubuntu-18.04.tar.xztar-xfclang+llvm-15.0.0-x86_64-linux-gnu-ubuntu-18.04.tar.xz-C/optmv/opt/clang+llvm-15.0.0-x86_64-linux-gnu-ubuntu-18.04/opt/clangd_15.0.0#设置环境变量echo'exportPATH="/opt/clangd_15.0.0/bin:$PATH"'>>~/.bashrcsource~/.bashrc```###验证安装```bashclangd--version```正常输出:```clangdversion15.0.0```###备选镜像源如果官方下载速度较慢,可以考虑以下镜像站:-**清华大学镜像站**:https://mirrors.tuna.tsinghua.edu.cn/github-release/llvm/llvm-project/llvmorg-15.0.0/-**阿里云镜像站**:https://mirrors.aliyun.com/llvm-releases/downloads/15.0.0/###注意事项1.编译包是基于Ubuntu18.04构建的,在其他Linux发行版(如CentOS、SUSE)上可能遇到glibc版本问题。如果遇到兼容性问题,可能需要从源码编译(参考引用[1]和[2])。2.如果系统缺少依赖库,可能需要安装相应的库。例如,在CentOS上可能需要安装`libstdc++`等。§§相关问题§§1.如何解决在低版本glibc系统(如CentOS7)上运行clangd15.0.0的问题?2.如何从源码编译clangd15.0.0(含详细步骤)?3.如何为clangd配置多个版本(如同时安装13.0.0和15.0.0)并切换?4.在VSCode中如何指定特定版本的clangd路径?</think>以下是 **clangd 15.0.0** 的 Linux 预编译包信息(x86_64 架构): ### 官方下载地址 **clangd 15.0.0 Linux 预编译(x86_64)**: 👉 [https://github.com/llvm/llvm-project/releases/download/llvmorg-15.0.0/clang+llvm-15.0.0-x86_64-linux-gnu-ubuntu-18.04.tar.xz](https://github.com/llvm/llvm-project/releases/download/llvmorg-15.0.0/clang+llvm-15.0.0-x86_64-linux-gnu-ubuntu-18.04.tar.xz) ### 安装步骤 ```bash # 下载并解压 wget https://github.com/llvm/llvm-project/releases/download/llvmorg-15.0.0/clang+llvm-15.0.0-x86_64-linux-gnu-ubuntu-18.04.tar.xz tar -xf clang+llvm-15.0.0-x86_64-linux-gnu-ubuntu-18.04.tar.xz -C /opt mv /opt/clang+llvm-15.0.0-x86_64-linux-gnu-ubuntu-18.04 /opt/clangd_15.0.0 # 添加环境变量 echo 'export PATH="/opt/clangd_15.0.0/bin:$PATH"' >> ~/.bashrc source ~/.bashrc ``` ### 验证安装 ```bash clangd --version ``` 输出应显示: ``` clangd version 15.0.0 ``` ### 备选镜像源(加速下载) - **清华大学镜像站**: [https://mirrors.tuna.tsinghua.edu.cn/github-release/llvm/llvm-project/llvmorg-15.0.0/](https://mirrors.tuna.tsinghua.edu.cn/github-release/llvm/llvm-project/llvmorg-15.0.0/) - **阿里云镜像站**: [https://mirrors.aliyun.com/llvm-releases/downloads/15.0.0/](https://mirrors.aliyun.com/llvm-releases/downloads/15.0.0/) --- ### 注意事项 1. **兼容性**:此包基于 Ubuntu 18.04 构建,在 CentOS/RHEL 等系统可能需额外依赖: ```bash # CentOS/RHEL 依赖 sudo yum install libstdc++ ncurses-compat-libs ``` 2. **源码编译**(预编译包不兼容时): 参考引用[1][2]的编译方法: ```bash git clone --depth 1 --branch llvmorg-15.0.0 https://github.com/llvm/llvm-project.git cd llvm-project mkdir build && cd build cmake ../llvm -DCMAKE_BUILD_TYPE=Release -DLLVM_ENABLE_PROJECTS="clang;clang-tools-extra" make -j$(nproc) clangd ``` ---
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值