cgo 交叉编译错误 stdlib.h 未找到

cgo cross compile error stdlib.h not found

背景

我准备用go-sqlite3交叉编译golang应用,这让我陷入了“cgo交叉编译的坑”。为了交叉编译成aarch64,我使用的命令如下,但是错误:

[root@ec5dc2a3bd37 data_collector]# make crosscompiletry
CGO_ENABLED=1 GOOS=linux GOARCH=arm64 CC=aarch64-linux-gnu-gcc CXX=aarch64-linux-gnu-g++ AR=aarch64-linux-gnu-ar CGO_LDFLAGS="--sysroot=/usr/include:/usr/lib/gcc/aarch64-linux-gnu/4.8.5/include -static" /usr/local/go/bin/go build -o ./output/crosscompile/dcagent ./cmd/agent/main.go
# runtime/cgo
_cgo_export.c:3:20: fatal error: stdlib.h: No such file or directory
 #include <stdlib.h>
                    ^
compilation terminated.
make: *** [crosscompiletry] Error 2

问题

我该如何解决这个错误?

这是否意味着 gccgo 找不到 aarch64 的包含文件?

我有那些包含和开发工具,包含是由 --sysroot 设置的,我做对了吗?

我会在等待帮助的时候挖掘如下线索:

  1. 我找到了 this step 但我没有这样做,这与我的问题有关吗?
  2. 在网上搜索了还是没有找到解决办法,最类似的情况是this blog,但是如何在centos中安装musl-dev?
  3. 找到一个存储库xcgo,稍后会尝试。

附件

include_files

[root@ec5dc2a3bd37 data_collector]# find /usr -regex '.*aarch64.*std.*'
/usr/lib/gcc/aarch64-linux-gnu/4.8.5/include/stdnoreturn.h
/usr/lib/gcc/aarch64-linux-gnu/4.8.5/include/stdarg.h
/usr/lib/gcc/aarch64-linux-gnu/4.8.5/include/stdbool.h
/usr/lib/gcc/aarch64-linux-gnu/4.8.5/include/stdint.h
/usr/lib/gcc/aarch64-linux-gnu/4.8.5/include/stdalign.h
/usr/lib/gcc/aarch64-linux-gnu/4.8.5/include/stddef.h
/usr/lib/gcc/aarch64-linux-gnu/4.8.5/include/stdint-gcc.h
/usr/lib/gcc/aarch64-linux-gnu/4.8.5/include/stdfix.h
[root@ec5dc2a3bd37 data_collector]# find /usr -regex '.*stdlib.*'
/usr/include/c++/4.8.2/tr1/stdlib.h
/usr/include/c++/4.8.2/tr1/cstdlib
/usr/include/c++/4.8.2/cstdlib
/usr/include/stdlib.h
/usr/include/bits/stdlib.h
/usr/include/bits/stdlib-float.h
/usr/include/bits/stdlib-ldbl.h
/usr/local/go/src/go/types/stdlib_test.go

gcc_g++_工具链

[root@ec5dc2a3bd37 data_collector]# yum groupinstall "Development Tools"
Loaded plugins: fastestmirror, ovl
Loading mirror speeds from cached hostfile
 * base: mirrors.163.com
 * epel: mirror.lzu.edu.cn
 * extras: mirrors.163.com
 * updates: mirrors.163.com
Maybe run: yum groups mark install (see man yum)
No packages in any requested group available to install or update

go_env

[root@ec5dc2a3bd37 data_collector]# go env | grep -v -E 'GOMOD|GONOPROXY|GOPRIVATE|GOPROXY'
GO111MODULE="on"
GOARCH="amd64"
GOBIN=""
GOCACHE="/root/.cache/go-build"
GOENV="/root/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GONOSUMDB="*"
GOOS="linux"
GOPATH="/root/go"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build259696924=/tmp/go-build -gno-record-gcc-switches"

centos7

[root@ec5dc2a3bd37 data_collector]# cat /etc/*release
CentOS Linux release 7.9.2009 (Core)

我使用 xcgo 解决了我的问题。我的编译命令是:

apt install gcc-8-aarch64-linux-gnu

CGO_ENABLED=1 GOOS=linux GOARCH=arm64 CC=aarch64-linux-gnu-gcc-8 CGO_LDFLAGS="-static" $(GO) build

Return对于这个问题,我觉得centos安装交叉编译工具有一些细微的bug,导致报错beyond。如果有人看到这个,建议用 xcgo 交叉编译你的应用程序,它支持 go 1.15。即使您想自己构建交叉编译 docker,基于 ubuntu 也是一个好主意。

因为 'go build' 是高级编译命令,我不知道如何将 '-debug-gcc' 传递给 'go tool cgo' 以打印更多调试信息。找到一种打印更多调试信息的方法应该是深入研究的好选择,我会稍后再做。

谢谢。

sudo apt install --reinstall build-essential

为我解决了错误。