从源码构建Golang编译器时遇到的问题

从源码构建Golang编译器时遇到的问题 我按照 https://golang.org/doc/install/source 上的说明,在我的 Windows 10 WSL 中从源代码构建 Go 编译器,但遇到了以下错误:

运行 ./all.bash

> Building Go toolchain1 using /usr/local/go.
> Building Go bootstrap cmd/go (go_bootstrap) using Go toolchain1.
> Building Go toolchain2 using go_bootstrap and Go toolchain1.
> Building Go toolchain3 using go_bootstrap and Go toolchain2.
> go install go/token: copying /tmp/go-build3158513450/b083/_pkg_.a: open /mnt/d/software_dev/go/Go_compiler/goroot/pkg/linux_amd64/go/token.a: no such file or directory
> go install cmd/internal/obj/s390x: copying /tmp/go-build3158513450/b063/_pkg_.a: open /mnt/d/software_dev/go/Go_compiler/goroot/pkg/linux_amd64/cmd/internal/obj/s390x.a: no such file or directory
> go tool dist: FAILED: /mnt/d/software_dev/go/Go_compiler/goroot/pkg/tool/linux_amd64/go_bootstrap install -gcflags=all= -ldflags=all= -a -i cmd/asm cmd/cgo cmd/compile cmd/link: exit status 1

Go 版本为 go1.16 linux/amd64


更多关于从源码构建Golang编译器时遇到的问题的实战教程也可以访问 https://www.itying.com/category-94-b0.html

8 回复

WSL 1 还是 2?

更多关于从源码构建Golang编译器时遇到的问题的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html


哎呀! 我的错。没注意到 WSL。

WSL 更像是 Linux 而非 Windows。据我所知,WSL 无法运行 bat 文件。

你说得对。 我看到了问题,点击链接后看到了适用于 Windows 的 all.bat(忽略了 WSL)。抱歉!

根据您分享的链接(从源代码安装 Go - Go 编程语言):

(在 Windows 下构建请使用 all.bat。)

我按照从源代码安装Go的说明,在Windows 10的WSL上从源代码构建Go编译器。

我没有遇到你的问题。这对我来说是可行的。我无法复现你的问题,因为你只展示了你所做操作的一小部分。

(要在Windows下构建,请使用 all.bat。)

问题是关于

Windows 10 的 WSL

因此,我针对 Windows 10 WSL 进行了回复。

Microsoft Windows [Version 10.0.19042.804]
C:\Users\petrus>wsl
petrus:~$ uname -a
Linux petrus 5.4.72-microsoft-standard-WSL2 #1 SMP Wed Oct 28 23:40:43 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux
petrus:~$

然而,你回复的是针对 Windows 的。为什么?

这个错误通常是由于构建过程中目录权限或路径问题导致的。以下是解决方案:

  1. 清理并重新构建
cd /mnt/d/software_dev/go/Go_compiler/goroot/src
rm -rf ../pkg ../bin
./all.bash
  1. 如果问题仍然存在,尝试使用dist工具手动构建
cd /mnt/d/software_dev/go/Go_compiler/goroot/src
./make.bash
  1. 检查文件系统权限(WSL中常见问题):
ls -la /mnt/d/software_dev/go/Go_compiler/goroot/pkg/
chmod -R 755 /mnt/d/software_dev/go/Go_compiler/goroot/
  1. 使用更完整的清理方式
cd /mnt/d/software_dev/go/Go_compiler
rm -rf goroot
git clone https://go.googlesource.com/go goroot
cd goroot/src
./all.bash
  1. 如果是在WSL的/mnt目录下,尝试在WSL原生文件系统中构建
cd ~
mkdir go-build
cd go-build
git clone https://go.googlesource.com/go goroot
cd goroot/src
./all.bash
  1. 检查Go bootstrap版本兼容性
# 确保使用的bootstrap Go版本正确
/usr/local/go/bin/go version
# 应该是1.4或更高版本

构建成功后,验证安装:

cd /mnt/d/software_dev/go/Go_compiler/goroot
./bin/go version
回到顶部