当前位置:   article > 正文

在WSL2上安装新版本LLVM的方法_wsl下载不了llvm

wsl下载不了llvm

在WSL2上安装新版本LLVM的方法

我的wsl2安装的debian,不过本教程适用于debian和ubuntu。

一、检查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/"
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

其它版本,可以查看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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88

建议大家直接进入apt llvm网站查看最新信息。因为我的是debian 12,所以稍后我将添加相应版本的连接。

二、添加仓库到apt仓库列表中

apt的仓库位于/etc/apt/sources.list中,可以使用add-apt-repository命令(需要安装)添加。但是我更喜欢简单直接的方法:

sudo nano /etc/apt/sources.list
  • 1

上面的命令行将以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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

请确保版本一致。比如你可以看到我的debian是bookwork版本(12),所以llvm中也有bookworm字样。我是两个都添加了。

如果此时你直接sudo apt update应该会提示缺少public key。所以还需要安装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
  • 1
  • 2
  • 3
  • 4

我是用第一种方法后,在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
  • 1
  • 2

更新完仓库可以使用下面的命令搜索是否有新版本的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

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52

对于不懂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
  • 1

除了这些也可以安装:

sudo apt install python3-clang-17 libclang-17-dev clang-format-17 clang-tools-17 clang-tidy-17
  • 1

我安装的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'
  • 1
  • 2
  • 3
  • 4

这样当我使用clang也可以访问相应的程序。

当然对于编程如果要默认使用clang也可以这样设置:

export CC=/usr/bin/clang-17
export CXX=/usr/bin/clang++17
  • 1
  • 2
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/510150
推荐阅读
相关标签
  

闽ICP备14008679号