在 WSL2 中编译 LLVM遇到了如下报错:
collect2: fatal error: ld terminated with signal 9 [Killed]
经过查询,发现是因为内存不足,造成进程被killed。
默认WSL2 的内存是主机内存的50%或者8GB。因此提高内存上限即可。如果主机内存有限,可以增大swap的大小。默认 swap大小为内存的25%。
WSL2 的默认配置文件是:C:\Users\<UserName>\.wslconfig
,其中 UserName 为相对应的用户名。默认不存在这个文件,手动创建一个即可。
方案1:提高默认内存上限
默认WSL2 的内存是主机内存的50%或者8GB。
# Settings apply across all Linux distros running on WSL 2
[wsl2]# Limits VM memory to use no more than 4 GB, this can be set as whole numbers using GB or MB
memory=16GB
方案2:增加swap大小
默认 swap大小为内存的25%。
# Settings apply across all Linux distros running on WSL 2
[wsl2]# Sets amount of swap storage space to 8GB, default is 25% of available RAM
swap=16GB
然后重启WSL即可。
# 关闭wsl
wsl --shutdown# 启用 wsl
wsl# 查询 内存大小和 swap 大小
free -h
参考:
- Build failing on WSL2 Ubuntu 20.04
- https://learn.microsoft.com/en-us/windows/wsl/wsl-config