使用 GOOS=Linux 在 macOS 上进行 libvirt-go 交叉编译失败

libvirt-go cross-compiling fails on macos with the GOOS=Linux

我有一个项目使用 libvirt-go v7.4.0.

go.mod:

github.com/libvirt/libvirt-go v7.4.0+incompatible // indirect

它在 mac 和 linux 上编译,但是 cross-compiling 在 macos 上失败 GOOS=Linux旗帜.

$ env GOOS=linux go build -o myapp .
# gitlab.mycompany.io/mycompany-platform/myapp/command/agent
command/agent/libvirt_listener.go:11:26: undefined: libvirt.DomainEventType
command/agent/libvirt_listener.go:23:14: undefined: libvirt.Connect
command/agent/libvirt_listener.go:100:9: undefined: libvirt.EventRegisterDefaultImpl
command/agent/libvirt_listener.go:105:15: undefined: libvirt.NewConnect
command/agent/libvirt_listener.go:115:4: undefined: libvirt.EventRunDefaultImpl
command/agent/libvirt_listener.go:121:22: undefined: libvirt.Connect
command/agent/libvirt_listener.go:121:42: undefined: libvirt.Domain
command/agent/libvirt_listener.go:121:65: undefined: libvirt.DomainEventLifecycle
command/agent/libvirt_listener.go:149:63: undefined: libvirt.DomainState
command/agent/libvirt_listener.go:121:22: too many errors

libvirt-go 包是一个绑定到原生 libvirt 平台库的 CGo。因此,在构建 CGO 时不可能禁用它,反过来也不可能交叉编译,因为这会隐式禁用 CGO。