Yocto嵌入式系统中Go编译器运行项目报错求助
Yocto嵌入式系统Go编译标准库缺失问题解决
问题描述
在Yocto构建的嵌入式系统中添加Go编译器recipe后,运行Go项目时出现标准库找不到的错误:
cmd/updater/main.go:8:2: package text/tabwriter is not in GOROOT (/usr/lib/go/src/text/tabwriter) ../go/pkg/mod/golang.org/x/sys@v0.3.0/unix/syscall_linux.go:18:2: package time is not in GOROOT (/usr/lib/go/src/time) ../go/pkg/mod/github.com/godbus/dbus/v5@v5.0.3-0.20190910164622-6eb4cf171088/variant_lexer.go:6:2: package unicode is not in GOROOT (/usr/lib/go/src/unicode) ../go/pkg/mod/golang.org/x/term@v0.3.0/terminal.go:13:2: package unicode/utf8 is not in GOROOT (/usr/lib/go/src/unicode/utf8) ../go/pkg/mod/golang.org/x/sys@v0.3.0/unix/affinity_linux.go:11:2: package unsafe is not in GOROOT (/usr/lib/go/src/unsafe)
已尝试设置GOROOT/GOPATH环境变量、go mod tidy/go mod download/go mod vendor等操作,问题未解决。
原因分析
Yocto默认构建的Go编译器包为了精简嵌入式镜像,仅包含编译器和运行时,未安装标准库的源码文件。而Go编译部分项目时需要访问GOROOT/src下的标准库源码,导致找不到包的错误。
解决步骤
修改Yocto构建配置,启用标准库源码安装:
在项目的local.conf文件中添加配置项:GO_INSTALL_SRC = "1"该配置会让Go recipe在构建时将标准库源码打包进镜像。
重新构建并烧录镜像:
执行bitbake命令重新生成镜像:bitbake <你的镜像名称>完成后将新镜像烧录到嵌入式设备中。
确认环境变量一致性:
在设备的/etc/profile或~/.bashrc中添加全局环境变量配置:export GOROOT=/usr/lib/go export PATH=$PATH:$GOROOT/bin执行
source /etc/profile(或对应文件)使配置生效,确保shell环境与Go工具的GOROOT设置一致。验证标准库安装:
在设备上执行以下命令检查标准库文件是否存在:ls /usr/lib/go/src/text/tabwriter若能看到该目录下的源码文件,说明标准库已正确安装。
重新编译Go项目:
回到项目目录执行go build,此时应能正常编译,不再出现标准库缺失的错误。
内容的提问来源于stack exchange,提问作者Thugmek
相关产品推荐
相关产品推荐

