赞
踩
我的wsl2安装的debian,不过本教程适用于debian和ubuntu。
使用命令检查debian版本:
$ cat /etc/os-release
PRETTY_NAME="Debian GNU/Linux 12 (bookworm)"
NAME="Debian GNU/Linux"
VERSION_ID="12"
VERSION="12 (bookworm)"
VERSION_CODENAME=bookworm
ID=debian
HOME_URL="https://www.debian.org/"
SUPPORT_URL="https://www.debian.org/support"
BUG_REPORT_URL="https://bugs.debian.org/"
其它版本,可以查看llvm apt查看详情:
Debian Buster (Debian 10 - old-old-stable) - Last update : Thu, 22 Feb 2024 15:18:03 UTC / Revision: 20240222115549+f17e4151423a deb http://apt.llvm.org/buster/ llvm-toolchain-buster main deb-src http://apt.llvm.org/buster/ llvm-toolchain-buster main # 17 deb http://apt.llvm.org/buster/ llvm-toolchain-buster-17 main deb-src http://apt.llvm.org/buster/ llvm-toolchain-buster-17 main # 18 deb http://apt.llvm.org/buster/ llvm-toolchain-buster-18 main deb-src http://apt.llvm.org/buster/ llvm-toolchain-buster-18 main Bullseye (Debian 11 - old-stable) - Last update : Wed, 21 Feb 2024 05:29:27 UTC / Revision: 20240219100934+1f57800fe6ff deb http://apt.llvm.org/bullseye/ llvm-toolchain-bullseye main deb-src http://apt.llvm.org/bullseye/ llvm-toolchain-bullseye main # 17 deb http://apt.llvm.org/bullseye/ llvm-toolchain-bullseye-17 main deb-src http://apt.llvm.org/bullseye/ llvm-toolchain-bullseye-17 main # 18 deb http://apt.llvm.org/bullseye/ llvm-toolchain-bullseye-18 main deb-src http://apt.llvm.org/bullseye/ llvm-toolchain-bullseye-18 main Bookworm (Debian 12 - stable) - Last update : Mon, 19 Feb 2024 07:36:09 UTC / Revision: 20240218105224+dc94eb57e39a deb http://apt.llvm.org/bookworm/ llvm-toolchain-bookworm main deb-src http://apt.llvm.org/bookworm/ llvm-toolchain-bookworm main # 17 deb http://apt.llvm.org/bookworm/ llvm-toolchain-bookworm-17 main deb-src http://apt.llvm.org/bookworm/ llvm-toolchain-bookworm-17 main # 18 deb http://apt.llvm.org/bookworm/ llvm-toolchain-bookworm-18 main deb-src http://apt.llvm.org/bookworm/ llvm-toolchain-bookworm-18 main Trixie / sid (unstable) - Last update : Fri, 23 Feb 2024 01:19:40 UTC / Revision: 20240222101030+ae3e14276b71 deb http://apt.llvm.org/unstable/ llvm-toolchain main deb-src http://apt.llvm.org/unstable/ llvm-toolchain main # 17 deb http://apt.llvm.org/unstable/ llvm-toolchain-17 main deb-src http://apt.llvm.org/unstable/ llvm-toolchain-17 main # 18 deb http://apt.llvm.org/unstable/ llvm-toolchain-18 main deb-src http://apt.llvm.org/unstable/ llvm-toolchain-18 main Ubuntu Precise, Quantal, Raring, Saucy, Utopic, Artful, Cosmic, Eoan and Trusty are no longer supported by Ubuntu. Repo remains available but not updated. As i386 isn't supported by Ubuntu anymore, apt.llvm.org isn't either. Bionic LTS (18.04) - Last update : Thu, 22 Feb 2024 20:38:55 UTC / Revision: 20240222064029+5c24c316496e deb http://apt.llvm.org/bionic/ llvm-toolchain-bionic main deb-src http://apt.llvm.org/bionic/ llvm-toolchain-bionic main # Needs 'sudo add-apt-repository ppa:ubuntu-toolchain-r/test' for libstdc++ with C++20 support # 17 deb http://apt.llvm.org/bionic/ llvm-toolchain-bionic-17 main deb-src http://apt.llvm.org/bionic/ llvm-toolchain-bionic-17 main # 18 deb http://apt.llvm.org/bionic/ llvm-toolchain-bionic-18 main deb-src http://apt.llvm.org/bionic/ llvm-toolchain-bionic-18 main Focal (20.04) LTS - Last update : Thu, 22 Feb 2024 19:37:37 UTC / Revision: 20240222053038+26d71d9ed56c deb http://apt.llvm.org/focal/ llvm-toolchain-focal main deb-src http://apt.llvm.org/focal/ llvm-toolchain-focal main # 17 deb http://apt.llvm.org/focal/ llvm-toolchain-focal-17 main deb-src http://apt.llvm.org/focal/ llvm-toolchain-focal-17 main # 18 deb http://apt.llvm.org/focal/ llvm-toolchain-focal-18 main deb-src http://apt.llvm.org/focal/ llvm-toolchain-focal-18 main Jammy (22.04) LTS - Last update : Thu, 22 Feb 2024 17:22:11 UTC / Revision: 20240222031214+307409a8872f deb http://apt.llvm.org/jammy/ llvm-toolchain-jammy main deb-src http://apt.llvm.org/jammy/ llvm-toolchain-jammy main # 17 deb http://apt.llvm.org/jammy/ llvm-toolchain-jammy-17 main deb-src http://apt.llvm.org/jammy/ llvm-toolchain-jammy-17 main # 18 deb http://apt.llvm.org/jammy/ llvm-toolchain-jammy-18 main deb-src http://apt.llvm.org/jammy/ llvm-toolchain-jammy-18 main Lunar (23.04) - Last update : Thu, 22 Feb 2024 18:29:36 UTC / Revision: 20240222042526+43f1fa99ca7d deb http://apt.llvm.org/lunar/ llvm-toolchain-lunar main deb-src http://apt.llvm.org/lunar/ llvm-toolchain-lunar main # 17 deb http://apt.llvm.org/lunar/ llvm-toolchain-lunar-17 main deb-src http://apt.llvm.org/lunar/ llvm-toolchain-lunar-17 main # 18 deb http://apt.llvm.org/lunar/ llvm-toolchain-lunar-18 main deb-src http://apt.llvm.org/lunar/ llvm-toolchain-lunar-18 main Mantic (23.10) - Last update : Thu, 22 Feb 2024 22:42:22 UTC / Revision: 20240222083539+66f6929fec3a deb http://apt.llvm.org/mantic/ llvm-toolchain-mantic main deb-src http://apt.llvm.org/mantic/ llvm-toolchain-mantic main # 17 deb http://apt.llvm.org/mantic/ llvm-toolchain-mantic-17 main deb-src http://apt.llvm.org/mantic/ llvm-toolchain-mantic-17 main # 18 deb http://apt.llvm.org/mantic/ llvm-toolchain-mantic-18 main deb-src http://apt.llvm.org/mantic/ llvm-toolchain-mantic-18 main
建议大家直接进入apt llvm网站查看最新信息。因为我的是debian 12,所以稍后我将添加相应版本的连接。
apt的仓库位于/etc/apt/sources.list
中,可以使用add-apt-repository
命令(需要安装)添加。但是我更喜欢简单直接的方法:
sudo nano /etc/apt/sources.list
上面的命令行将以root权限编辑这个文本文件。然后复制17或者18版本连接到文件中。我修改之后这个文件的内容如下:
deb http://deb.debian.org/debian bookworm main
deb http://deb.debian.org/debian bookworm-updates main
deb http://security.debian.org/debian-security bookworm-security main
deb http://ftp.debian.org/debian bookworm-backports main
# LLVM 17
deb http://apt.llvm.org/bookworm/ llvm-toolchain-bookworm-17 main
deb-src http://apt.llvm.org/bookworm/ llvm-toolchain-bookworm-17 main
# LLVM 18
deb http://apt.llvm.org/bookworm/ llvm-toolchain-bookworm-18 main
deb-src http://apt.llvm.org/bookworm/ llvm-toolchain-bookworm-18 main
请确保版本一致。比如你可以看到我的debian是bookwork版本(12),所以llvm中也有bookworm字样。我是两个都添加了。
如果此时你直接sudo apt update
应该会提示缺少public key。所以还需要安装public key。
在llvm apt网页中有两种方法:
wget -O - https://apt.llvm.org/llvm-snapshot.gpg.key | sudo apt-key add -
# or
wget -qO- https://apt.llvm.org/llvm-snapshot.gpg.key | sudo tee /etc/apt/trusted.gpg.d/apt.llvm.org.asc
# Fingerprint: 6084 F3CF 814B 57C1 CF12 EFD5 15CF 4D18 AF4F 7421
我是用第一种方法后,在update apt有两个warning。所以我使用第二种方法:
$ wget -qO- https://apt.llvm.org/llvm-snapshot.gpg.key | sudo tee /etc/apt/trusted.gpg.d/apt.llvm.org.asc
$ sudo apt update
更新完仓库可以使用下面的命令搜索是否有新版本的llvm
$ sudo apt search llvm # 省略部分... clang-17/unknown 1:17.0.6~++20231208085813+6009708b4367-1~exp1~20231208085906.81 amd64 C, C++ and Objective-C compiler clang-17-doc/unknown 1:17.0.6~++20231208085813+6009708b4367-1~exp1~20231208085906.81 all C, C++ and Objective-C compiler - Documentation clang-17-examples/unknown 1:17.0.6~++20231208085813+6009708b4367-1~exp1~20231208085906.81 amd64 Clang examples clang-18/unknown 1:18.1.0~++20240218123237+82e17c153611-1~exp1~20240218003355.46 amd64 C, C++ and Objective-C compiler llvm-17/unknown 1:17.0.6~++20231208085813+6009708b4367-1~exp1~20231208085906.81 amd64 Modular compiler and toolchain technologies llvm-17-dbgsym/unknown 1:17.0.6~++20231208085813+6009708b4367-1~exp1~20231208085906.81 amd64 debug symbols for llvm-17 llvm-17-dev/unknown 1:17.0.6~++20231208085813+6009708b4367-1~exp1~20231208085906.81 amd64 Modular compiler and toolchain technologies, libraries and headers llvm-17-dev-dbgsym/unknown 1:17.0.6~++20231208085813+6009708b4367-1~exp1~20231208085906.81 amd64 debug symbols for llvm-17-dev llvm-17-doc/unknown 1:17.0.6~++20231208085813+6009708b4367-1~exp1~20231208085906.81 all Modular compiler and toolchain technologies, documentation llvm-17-examples/unknown 1:17.0.6~++20231208085813+6009708b4367-1~exp1~20231208085906.81 all Modular compiler and toolchain technologies, examples llvm-17-linker-tools/unknown 1:17.0.6~++20231208085813+6009708b4367-1~exp1~20231208085906.81 amd64 Modular compiler and toolchain technologies - Plugins llvm-17-linker-tools-dbgsym/unknown 1:17.0.6~++20231208085813+6009708b4367-1~exp1~20231208085906.81 amd64 debug symbols for llvm-17-linker-tools llvm-17-runtime/unknown 1:17.0.6~++20231208085813+6009708b4367-1~exp1~20231208085906.81 amd64 Modular compiler and toolchain technologies, IR interpreter llvm-17-runtime-dbgsym/unknown 1:17.0.6~++20231208085813+6009708b4367-1~exp1~20231208085906.81 amd64 debug symbols for llvm-17-runtime llvm-17-tools/unknown 1:17.0.6~++20231208085813+6009708b4367-1~exp1~20231208085906.81 amd64 Modular compiler and toolchain technologies, tools llvm-17-tools-dbgsym/unknown 1:17.0.6~++20231208085813+6009708b4367-1~exp1~20231208085906.81 amd64 debug symbols for llvm-17-tools llvm-18/unknown 1:18.1.0~++20240218123237+82e17c153611-1~exp1~20240218003355.46 amd64 Modular compiler and toolchain technologies
对于不懂nano操作的人需要说明的是,在关闭之前要先使用CTRL+O
快捷键保存。然后再使用CTRL+X
关闭文件。如果你修改错误也可以直接使用CTRL+X
关闭文件,只是在关闭的时候会提示你是否保存。你可以选择N,表示不保存修改改。和vim的操作逻辑差不多,只是按键不同。(因为我默认安装的vim不支持粘贴系统粘贴板的内容,所以使用了nano工具。)
我这里安装clangd clang和llvm。
我这里只安装clang和clangd。关于clangd的描述是:clangd is a language server that can work with many editors via a plugin.
.
sudo apt install clang-17 clangd-17 lldb-17 lld-17
除了这些也可以安装:
sudo apt install python3-clang-17 libclang-17-dev clang-format-17 clang-tools-17 clang-tidy-17
我安装的17版本的。我查看官方好像18.1今年晚些时候会发布。先选择一个稍微稳定的吧。
后面测试一些就成。这里不再赘述。
如果你直接使用clang --version
命令,会发现找不到clang。那是因为实际上你应该使用clang-17 --version
采用有正确的输出。
你的c++文件的编译工具是/usr/bin/clang++-17
或者/usr/bin/clang-cpp-17
,c文件的编译工具是/usr/bin/clang-17
. Linux的链接器是/usr/bin/ld.lld-17
,windows的是lld-link
。请注意不要忘了带-17
.当然如果你安装18版本要带-18
.
当然还有一种更好的办法是设置别名,比如我在~/.bashrc中添加了的如下设置:
alias clang='/usr/bin/clang-17'
alias clang++='/usr/bin/clang++-17'
alias ld.lld='/usr/bin/ld.lld-17'
alias lldb='/usr/bin/lldb-17'
这样当我使用clang也可以访问相应的程序。
当然对于编程如果要默认使用clang也可以这样设置:
export CC=/usr/bin/clang-17
export CXX=/usr/bin/clang++17
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。