cgo交叉编译错误,找不到stdlib.h。

huangapple go评论117阅读模式
英文:

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. 我发现了这个步骤,我没有执行,这与我的问题有关吗?
  2. 在搜索互联网后仍然没有找到解决方案,最相似的情况是这篇博客,但如何在CentOS上安装musl-dev?
  3. 发现了一个名为xcgo的存储库,稍后将尝试使用它。

附件

包含文件

[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环境

[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"

CentOS 7

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

background

I'm going to cross compile golang app using go-sqlite3, which push me into "cgo cross compile pit fall". In order to cross compile it to aarch64, the command I used is following, but error:

[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=&quot;--sysroot=/usr/include:/usr/lib/gcc/aarch64-linux-gnu/4.8.5/include -static&quot; /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 &lt;stdlib.h&gt;
                    ^
compilation terminated.
make: *** [crosscompiletry] Error 2

question

How can I fix this error?

Did it means that gccgo can't find includes for aarch64?

I have those includes and develop tools, includes are set by --sysroot, did I do right?

I will dig clues as following while waiting help:

  1. I found this step which I didn't do, is this related to my problem?
  2. After searched internet still not found solution, most similar situation is this blog, but how to install musl-dev in centos?
  3. found a repository xcgo, will try it later.

attachment

include_files

[root@ec5dc2a3bd37 data_collector]# find /usr -regex &#39;.*aarch64.*std.*&#39;
/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 &#39;.*stdlib.*&#39;
/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++_toolchain

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

centos7

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

答案1

得分: 3

以下是翻译好的内容:

sudo apt install --reinstall build-essential

这个命令对我解决了错误。

英文:
sudo apt install --reinstall build-essential

resolved the error for me.

答案2

得分: 2

我使用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

回到这个问题,我认为在CentOS中安装交叉编译工具时可能存在一些微妙的错误,导致了错误的报告。如果有人看到这个问题,建议使用xcgo进行交叉编译,它支持go 1.15。即使你想手动构建交叉编译的Docker镜像,基于Ubuntu也是一个不错的选择。

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

谢谢。

英文:

I solved my problem using xcgo. My compile command is :

apt install gcc-8-aarch64-linux-gnu

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

Return to this question, I think there are some subtle bug about cross compile tool installation in centos, which leads to error reported beyond. If some one saw this, It's recommanded to cross compile your app with xcgo, it support go 1.15. Even if you want build your cross compile docker by your hands, based on ubuntu would be a good idea.

Because 'go build' is a high level compile command, I don't know how to pass '-debug-gcc' to 'go tool cgo' in order to print more debug info. Finding a way to print more debuging info should be a good choice to dive deeper, and I would do it later.

Thanks.

huangapple
  • 本文由 发表于 2021年5月26日 16:17:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/67701074.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定