-
Notifications
You must be signed in to change notification settings - Fork 18.4k
Description
What version of Go are you using (go version
)?
$ go version go version go1.17.5 linux/amd64
Does this issue reproduce with the latest release?
Yes.
What operating system and processor architecture are you using (go env
)?
go env
Output
$ go env GO111MODULE="" GOARCH="amd64" GOBIN="/home/u0/Documents/monteur/.monteurFS/bin/gopath/bin" GOCACHE="/home/u0/Documents/monteur/.monteurFS/bin/gocache" GOENV="/home/u0/Documents/monteur/.monteurFS/bin/goenv" GOEXE="" GOEXPERIMENT="" GOFLAGS="" GOHOSTARCH="amd64" GOHOSTOS="linux" GOINSECURE="" GOMODCACHE="/home/u0/Documents/monteur/.monteurFS/bin/gopath/pkg/mod" GONOPROXY="" GONOSUMDB="" GOOS="linux" GOPATH="/home/u0/Documents/monteur/.monteurFS/bin/gopath" GOPRIVATE="" GOPROXY="https://proxy.golang.org,direct" GOROOT="/home/u0/Documents/monteur/.monteurFS/bin/golang" GOSUMDB="sum.golang.org" GOTMPDIR="" GOTOOLDIR="/home/u0/Documents/monteur/.monteurFS/bin/golang/pkg/tool/linux_amd64" GOVCS="" GOVERSION="go1.17.5" GCCGO="gccgo" AR="ar" CC="gcc" CXX="g++" CGO_ENABLED="1" GOMOD="/home/u0/Documents/monteur/gopkg/go.mod" 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/user/1000/go-build3616176871=/tmp/go-build -gno-record-gcc-switches"
What did you do?
So I try to cross-compile a pure Go repository to armv7
CPU on amd64
host system with -buildmode=pie
, the compiler keeps on seeking for arm based gcc compiler eventhough the entire project does not have cgo codes at all. The command I issued:
GOOS="linux" \
GOARCH="arm" \
GOARM="7" \
go build -buildmode=pie \
-ldflags "-s -w" \
-o "/home/u0/Documents/monteur/.monteurFS/tmp/build/linux-arm" \
"/home/u0/Documents/monteur/gopkg/app/monteur/main.go"
I even tried to explicitly instruct the compiler that I'm not using Cgo:
CGO_ENABLED=0 \
GOOS="linux" \
GOARCH="arm" \
GOARM="7" \
go build -buildmode=pie \
-ldflags "-s -w" \
-o "/home/u0/Documents/monteur/.monteurFS/tmp/build/linux-arm" \
"/home/u0/Documents/monteur/gopkg/app/monteur/main.go"
What did you expect to see?
Should be working fine without needing to install arm compiler tool-chain like the workaround (see below).
What did you see instead?
Both the above produced the following outputs:
# command-line-arguments
loadinternal: cannot find runtime/cgo
/home/u0/Documents/monteur/.monteurFS/bin/golang/pkg/tool/linux_amd64/link: running gcc failed: exit status 1
gcc: error: unrecognized command-line option ‘-marm’; did you mean ‘-mabm’?
There are 2 ways to workaround the issue:
Number 1: don't build pie
mode
GOOS="linux" \
GOARCH="arm" \
GOARM="7" \
go build \
-ldflags "-s -w" \
-o "/home/u0/Documents/monteur/.monteurFS/tmp/build/linux-arm" \
"/home/u0/Documents/monteur/gopkg/app/monteur/main.go"
However, some packagers are great at complaining non-pie binary (e.g. debuild
).
Number 2: install arm gcc toolchain
On Debian:
$ su
$ apt install gcc-arm-linux-gnueabi binutils-arm-linux-gnueabi -y
$ exit
Then:
CGO_ENABLED=0 \
GOOS="linux" \
GOARCH="arm" \
GOARM="7" \
CC=arm-linux-gnueabi-gcc \
go build \
-ldflags "-s -w" \
-o "/home/u0/Documents/monteur/.monteurFS/tmp/build/linux-arm" \
"/home/u0/Documents/monteur/gopkg/app/monteur/main.go"
Gone through Resources
Gone through the following resources but found no outcome yet... still searching...
- https://www.reddit.com/r/golang/comments/gszi0d/best_way_to_cross_compile_a_golang_app_onto_a/
- https://groups.google.com/g/golang-nuts/c/gQ6ArCTlPGU
- https://stackoverflow.com/questions/61515186/when-using-cgo-enabled-is-must-and-what-happens
- https://dubo-dubon-duponey.medium.com/a-beginners-guide-to-cross-compiling-static-cgo-pie-binaries-golang-1-16-792eea92d5aa
- https://groups.google.com/g/golang-nuts/c/cXhRsmNsMwo/m/VUZlLqo9AwAJ
- https://groups.google.com/g/golang-nuts/c/Jd9tlNc6jUE/m/Z9ldF6vPEAAJ