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

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

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

问题

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

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

错误消息:

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

主机系统:

  1. 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环境:

  1. GO111MODULE=""
  2. GOARCH="amd64"
  3. GOBIN=""
  4. GOCACHE="/root/.cache/go-build"
  5. GOENV="/root/.config/go/env"
  6. GOEXE=""
  7. GOEXPERIMENT=""
  8. GOFLAGS=""
  9. GOHOSTARCH="amd64"
  10. GOHOSTOS="linux"
  11. GOINSECURE=""
  12. GOMODCACHE="/root/go/pkg/mod"
  13. GONOPROXY=""
  14. GONOSUMDB=""
  15. GOOS="linux"
  16. GOPATH="/root/go"
  17. GOPRIVATE=""
  18. GOPROXY="https://proxy.golang.org,direct"
  19. GOROOT="/usr/lib/go-1.18"
  20. GOSUMDB="sum.golang.org"
  21. GOTMPDIR=""
  22. GOTOOLDIR="/usr/lib/go-1.18/pkg/tool/linux_amd64"
  23. GOVCS=""
  24. GOVERSION="go1.18"
  25. GCCGO="gccgo"
  26. GOAMD64="v1"
  27. AR="ar"
  28. CC="gcc"
  29. CXX="g++"
  30. CGO_ENABLED="1"
  31. GOMOD="/home/worker/go.mod"
  32. GOWORK=""
  33. CGO_CFLAGS="-g -O2"
  34. CGO_CPPFLAGS=""
  35. CGO_CXXFLAGS="-g -O2"
  36. CGO_FFLAGS="-g -O2"
  37. CGO_LDFLAGS="-g -O2"
  38. PKG_CONFIG="pkg-config"
  39. 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:

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

Host System:

  1. 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:

  1. GO111MODULE=""
  2. GOARCH="amd64"
  3. GOBIN=""
  4. GOCACHE="/root/.cache/go-build"
  5. GOENV="/root/.config/go/env"
  6. GOEXE=""
  7. GOEXPERIMENT=""
  8. GOFLAGS=""
  9. GOHOSTARCH="amd64"
  10. GOHOSTOS="linux"
  11. GOINSECURE=""
  12. GOMODCACHE="/root/go/pkg/mod"
  13. GONOPROXY=""
  14. GONOSUMDB=""
  15. GOOS="linux"
  16. GOPATH="/root/go"
  17. GOPRIVATE=""
  18. GOPROXY="https://proxy.golang.org,direct"
  19. GOROOT="/usr/lib/go-1.18"
  20. GOSUMDB="sum.golang.org"
  21. GOTMPDIR=""
  22. GOTOOLDIR="/usr/lib/go-1.18/pkg/tool/linux_amd64"
  23. GOVCS=""
  24. GOVERSION="go1.18"
  25. GCCGO="gccgo"
  26. GOAMD64="v1"
  27. AR="ar"
  28. CC="gcc"
  29. CXX="g++"
  30. CGO_ENABLED="1"
  31. GOMOD="/home/worker/go.mod"
  32. GOWORK=""
  33. CGO_CFLAGS="-g -O2"
  34. CGO_CPPFLAGS=""
  35. CGO_CXXFLAGS="-g -O2"
  36. CGO_FFLAGS="-g -O2"
  37. CGO_LDFLAGS="-g -O2"
  38. PKG_CONFIG="pkg-config"
  39. 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:

确定