步骤 1:准备系统
确保你的系统已经更新,并且你安装了必要的工具:
sudo apt update
sudo apt upgrade
sudo apt install build-essential cmake git
这些步骤会确保你有最新的系统更新和构建工具。
步骤 2:获取LLVM 11 源代码
你有两种选择:使用 Git 克隆源代码,或者从官网下载压缩包。
(a) 通过 Git 克隆 LLVM 11 源代码
- 克隆 LLVM 11 源代码:
cd ~
git clone https://github.com/llvm/llvm-project.git
cd llvm-project
git checkout llvmorg-11.0.0 # 切换到LLVM 11版本
或者LLVM6:
cd ~/LLVM/llvm-project
git checkout llvmorg-6.0.1 # 切换到LLVM 6的版本
步骤 3:配置构建目录
无论你是通过 Git 克隆还是下载源码包,都需要创建一个单独的构建目录并配置 CMake
:
cd ~/llvm-project # 如果是通过Git克隆的,进入llvm-project目录
mkdir build
cd build
要清除 build
目录中的所有内容,可以通过以下命令:
- 进入
build
目录(如果你还没有进入的话):
cd ~/LLVM/llvm-project/build
- 清除
build
目录中的所有文件:
rm -rf *
这将删除 build
目录中的所有文件和子目录,包括 CMake 配置文件、编译过程中生成的文件等。
- 或者,你也可以直接删除整个
build
目录:
cd ~/LLVM/llvm-project
rm -rf build
然后重新创建 build
目录并继续后续的步骤:
mkdir build
cd build
清除完成后,你可以从头开始配置和编译 LLVM。
步骤 4:使用 CMake
配置项目
接下来,运行 CMake
来配置 LLVM 的构建:
cmake -G "Unix Makefiles" ../llvm
这里我第一次make只构建了llvm-configure,但是并没有clang,所以尝试指定clang重新编译。
cmake -G "Unix Makefiles" ../llvm -DCMAKE_INSTALL_PREFIX=/usr/local -DLLVM_ENABLE_PROJECTS="clang"
这个命令会生成用于编译LLVM的 Makefile 文件。
步骤 5:编译 LLVM
运行 make
来开始编译 LLVM: # 使用所有 CPU 核心来加速编译
make -j$(nproc)
这个过程可能需要一些时间,具体取决于你的硬件配置。
步骤 6:安装 LLVM 11
编译完成后,使用 make install
安装 LLVM:
sudo make install
这会将编译好的 LLVM 安装到系统的默认路径(通常是 /usr/local
)。
步骤 7:验证安装
安装完成后,验证 LLVM 是否已正确安装:
clang --version
llvm-config --version
你应该看到类似于以下的输出,表明 LLVM 11 已成功安装:
clang version 11.0.0 (https://github.com/llvm/llvm-project.git 7b8eb6a1b5e436d9ff4f8bda0ab8edbb6a234aad)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /usr/local/bin
步骤 8:配置环境变量(如果需要)
如果你在安装时没有修改默认路径,LLVM 工具应该已经安装到 /usr/local/bin
,你可以通过以下命令将它添加到 PATH
环境变量中:
export PATH=$PATH:/usr/local/bin
如果你希望每次启动终端时都自动配置,可以将它添加到 ~/.bashrc
或 ~/.bash_profile
文件中。
步骤 9:(可选)设置多个 LLVM 版本
如果你同时安装了多个版本的 LLVM(比如 LLVM 20 和 LLVM 11),你可以使用 update-alternatives
配置默认的 clang
和 llvm-config
版本。
sudo update-alternatives --install /usr/bin/clang clang /usr/local/bin/clang-11 100
sudo update-alternatives --install /usr/bin/llvm-config llvm-config /usr/local/bin/llvm-config-11 100
然后,你可以选择默认的版本:
sudo update-alternatives --config clang
sudo update-alternatives --config llvm-config
步骤 10:后续检查
如果一切顺利,你应该已经成功安装了 LLVM 11。可以再次运行以下命令来确认版本:
clang --version
llvm-config --version
如果遇到任何问题,随时告诉我,我会帮助你解决!
base) eric@Eric:~/LLVM/llvm-project$ git checkout llvmorg-11.0.0
Updating files: 100% (136546/136546), done.
Note: switching to 'llvmorg-11.0.0'.
You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by switching back to a branch.
If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -c with the switch command. Example:
git switch -c <new-branch-name>
Or undo this operation with:
git switch -
Turn off this advice by setting config variable advice.detachedHead to false
HEAD is now at 176249bd6732 [CodeGen][TailDuplicator] Don't duplicate blocks with INLINEASM_BR
(base) eric@Eric:~/LLVM/llvm-project$
[100%] Linking CXX executable ../../bin/opt
[100%] Built target llvm-dwp
[100%] Built target llvm-isel-fuzzer
[100%] Built target llvm-lto2
[100%] Built target llvm-opt-fuzzer
[100%] Built target opt
(base) eric@Eric:~/LLVM/llvm-project/build$