2

我正在尝试在两者linuxwindwos机器上发布我的项目但没有成功。

我试图明确定义-CC少数选项,但它们都不能在两台机器上工作。

预装包:

sudo apt-get install build-essential sudo apt-get install gcc-multilib g++-multilib sudo apt-get install gcc-mingw-w64

在项目中,我正在使用c使用这些代码flags进行选择性编译

//#cgo windows CFLAGS: "-IC:/Program Files/OpenSSL-Win64/include"
//#cgo windows LDFLAGS: "-LC:/Program Files/OpenSSL-Win64/lib" -llibcrypto
//#cgo linux LDFLAGS: -lssl -lcrypto
//#cgo CFLAGS: -Wno-deprecated-declarations

在机器上本地运行 GoRelaserwindows将导致此错误。

  ⨯ release failed after 24.28s error=failed to build for windows_arm64: exit status 2: # runtime/cgo
gcc_arm64.S: Assembler messages:
gcc_arm64.S:28: Error: no such instruction: `stp x29,x30,[sp,'
gcc_arm64.S:32: Error: too many memory references for `mov'
gcc_arm64.S:34: Error: no such instruction: `stp x19,x20,[sp,'
gcc_arm64.S:37: Error: no such instruction: `stp x21,x22,[sp,'
gcc_arm64.S:40: Error: no such instruction: `stp x23,x24,[sp,'
gcc_arm64.S:43: Error: no such instruction: `stp x25,x26,[sp,'
gcc_arm64.S:46: Error: no such instruction: `stp x27,x28,[sp,'
gcc_arm64.S:50: Error: too many memory references for `mov'
gcc_arm64.S:51: Error: too many memory references for `mov'
gcc_arm64.S:52: Error: too many memory references for `mov'
gcc_arm64.S:54: Error: no such instruction: `blr x20'
gcc_arm64.S:55: Error: no such instruction: `blr x19'
gcc_arm64.S:57: Error: no such instruction: `ldp x27,x28,[sp,'
gcc_arm64.S:60: Error: no such instruction: `ldp x25,x26,[sp,'
gcc_arm64.S:63: Error: no such instruction: `ldp x23,x24,[sp,'
gcc_arm64.S:66: Error: no such instruction: `ldp x21,x22,[sp,'
gcc_arm64.S:69: Error: no such instruction: `ldp x19,x20,[sp,'
gcc_arm64.S:72: Error: no such instruction: `ldp x29,x30,[sp],'

    ⨯ release failed after 24.28s error=failed to build for windows_arm64: exit status 2: # runtime/cgo
gcc_arm64.S: Assembler messages:
gcc_arm64.S:28: Error: no such instruction: `stp x29,x30,[sp,'
gcc_arm64.S:32: Error: too many memory references for `mov'
gcc_arm64.S:34: Error: no such instruction: `stp x19,x20,[sp,'
gcc_arm64.S:37: Error: no such instruction: `stp x21,x22,[sp,'
gcc_arm64.S:40: Error: no such instruction: `stp x23,x24,[sp,'
gcc_arm64.S:43: Error: no such instruction: `stp x25,x26,[sp,'
gcc_arm64.S:46: Error: no such instruction: `stp x27,x28,[sp,'
gcc_arm64.S:50: Error: too many memory references for `mov'
gcc_arm64.S:51: Error: too many memory references for `mov'
gcc_arm64.S:52: Error: too many memory references for `mov'
gcc_arm64.S:54: Error: no such instruction: `blr x20'
gcc_arm64.S:55: Error: no such instruction: `blr x19'
gcc_arm64.S:57: Error: no such instruction: `ldp x27,x28,[sp,'
gcc_arm64.S:60: Error: no such instruction: `ldp x25,x26,[sp,'
gcc_arm64.S:63: Error: no such instruction: `ldp x23,x24,[sp,'
gcc_arm64.S:66: Error: no such instruction: `ldp x21,x22,[sp,'
gcc_arm64.S:69: Error: no such instruction: `ldp x19,x20,[sp,'
gcc_arm64.S:72: Error: no such instruction: `ldp x29,x30,[sp],'

在机器上本地运行 GoReleaserubuntu将导致

gcc: error: unrecognized command line option ‘-mthreads’; did you mean ‘-pthread’?

发布者:

# This is an example .goreleaser.yml file with some sensible defaults.
# Make sure to check the documentation at https://goreleaser.com
before:
  hooks:
    # You may remove this if you don't use go modules.
    - go mod tidy
    # you may remove this if you don't need go generate
    - go generate ./...
builds:
  - env:
      - CGO_ENABLED=1
    goos:
      - linux
      - windows
archives:
  - replacements:
      linux: Linux
      windows: Windows
      386: i386
      amd64: x86_64
checksum:
  name_template: 'checksums.txt'
snapshot:
  name_template: "{{ incpatch .Version }}-next"
changelog:
  sort: asc
  filters:
    exclude:
      - '^docs:'
      - '^test:'
4

1 回答 1

0

默认情况下,GoReleaser 将尝试为 386、amd64 和 arm64 构建版本。但是你的 windows 机器就像 amd64/x86_64 机器。通常这对 Go 来说没有问题,但是由于您使用的是 CGO,因此您的 C 工具链必须支持 arm64 的编译,而它不支持。

goarch您应该能够通过添加到您的配置文件来限制它尝试构建的 CPU 架构:

...
builds:
  goarch:
    - amd64
...

交叉编译 cgo 项目是出了名的困难,也是 GoReleaser 的一个已知限制。如果您还没有这样做,我建议您查看cgo 食谱。

至于您在 ubuntu 上遇到的错误,看起来您的 C 工具链没有正确的依赖项/功能来交叉编译到 Windows。

我自己没有做过任何 CGO 交叉编译(试图不惜一切代价避免它)所以我不能保证这个工具,但从我在网上阅读的内容来看, xgo可能会帮助你。

于 2021-11-23T13:53:45.117 回答