如何使用cgo从linux/amd64交叉编译到darwin/arm64?

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

How to cross-compile from linux/amd64 to darwin/arm64 with cgo?

问题

我正在尝试在linux/amd64上交叉编译darwin/arm64,当我使用CGO_ENABLED=1构建代码时,出现了以下错误。我理解这个错误,但我不知道如何解决。

我的代码必须启用CGO_ENABLED功能进行编译。

错误消息:

GOOS=darwin GOARCH=arm64 CGO_ENABLED=1 CC=gcc go build -o dist/darwin-arm64 cmd/main.go
# runtime/cgo
gcc: error: arm64: No such file or directory
gcc: error: unrecognized command line option '-arch'
make: *** [makefile:6: darwin] Error 2

主机系统:

Linux 83433d127edb 5.13.0-28-generic #31~20.04.1-Ubuntu SMP Wed Jan 19 14:08:10 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux

Go环境:

GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/root/.cache/go-build"
GOENV="/root/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/root/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/root/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/lib/go-1.18"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/lib/go-1.18/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.18"
GCCGO="gccgo"
GOAMD64="v1"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/home/worker/go.mod"
GOWORK=""
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-build1206945591=/tmp/go-build -gno-record-gcc-switches"
英文:

I am trying to cross-compile darwin/arm64 on linux/amd64, I got the below error when I build code with CGO_ENABLED=1, I understand the error, but I don't how to solve.

My code must enable CGO_ENABLED feature to compile.

ErrorMessage:

GOOS=darwin GOARCH=arm64 CGO_ENABLED=1 CC=gcc go build -o dist/darwin-arm64 cmd/main.go
# runtime/cgo
gcc: error: arm64: No such file or directory
gcc: error: unrecognized command line option '-arch'
make: *** [makefile:6: darwin] Error 2

Host System:

Linux 83433d127edb 5.13.0-28-generic #31~20.04.1-Ubuntu SMP Wed Jan 19 14:08:10 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux

Go Environment:

GO111MODULE=""                                                                                                                       
GOARCH="amd64"                                                                                                                       
GOBIN=""                                                                                                                             
GOCACHE="/root/.cache/go-build"                                                                                                      
GOENV="/root/.config/go/env"                                                                                                         
GOEXE=""                                                                                                                             
GOEXPERIMENT=""                                                                                                                      
GOFLAGS=""                                                                                                                           
GOHOSTARCH="amd64"                                                                                                                   
GOHOSTOS="linux"                                                                                                                     
GOINSECURE=""                                                                                                                        
GOMODCACHE="/root/go/pkg/mod"                                                                                                        
GONOPROXY=""                                                                                                                         
GONOSUMDB=""                                                                                                                         
GOOS="linux"                                                                                                                         
GOPATH="/root/go"                                                                                                                    
GOPRIVATE=""                                                                                                                         
GOPROXY="https://proxy.golang.org,direct"                                                                                            
GOROOT="/usr/lib/go-1.18"                                                                                                            
GOSUMDB="sum.golang.org"                                                                                                             
GOTMPDIR=""                                                                                                                          
GOTOOLDIR="/usr/lib/go-1.18/pkg/tool/linux_amd64"                                                                                    
GOVCS=""                                                                                                                             
GOVERSION="go1.18"                                                                                                                   
GCCGO="gccgo"                                                                                                                        
GOAMD64="v1"                                                                                                                         
AR="ar"                                                                                                                              
CC="gcc"                                                                                                                             
CXX="g++"                                                                                                                            
CGO_ENABLED="1"                                                                                                                      
GOMOD="/home/worker/go.mod"                                                                                                          
GOWORK=""                                                                                                                            
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-build1206945591=/tmp/go-build -gno-record-gcc-switches"

答案1

得分: 2

很可能,你需要安装gcc工具链来进行到arm64的交叉编译。

我知道Debian/Ubuntu有用于Windows和其他Linux架构的交叉编译工具链(例如:gcc-aarch64-linux-gnulibc6-dev-arm64-crossgcc-mingw-w64-x86-64软件包)。

我不确定是否有可用于darwin的工具链。

英文:

Chances are, you need to install the gcc toolchain for cross-compilation to arm64.

I know Debian/Ubuntu have cross-compiling toolchains for Windows, as well as other Linux architectures (e.g.: packages gcc-aarch64-linux-gnu, libc6-dev-arm64-cross and gcc-mingw-w64-x86-64).

I'm not sure if there is a darwin toolchain available.

huangapple
  • 本文由 发表于 2022年3月19日 01:15:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/71530820.html
匿名

发表评论

匿名网友

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

确定