英文:
How to compile an amd64 binary that uses C on an M1 (arm64) Mac
问题
我的应用在将GOARCH
设置为arm64
(或省略)时可以正常编译。然而,当我尝试编译一个amd64二进制文件(GOOS=darwin GOARCH=amd64 go build
)时,我遇到了以下错误:
package [mainPackage]
imports [internalPackageThatUsesC]: build constraints exclude all Go files in [pathToInternalPackageThatUsesC]
[internalPackageThatUsesC]
包含3个文件 - 一个包含实现的Objective C文件,一个头文件以及一个提供本地代码接口的Go文件。以下是Go文件的内容:
// +build darwin
package [internalPackageThatUsesC]
/*
#cgo CFLAGS: -x objective-c
#cgo LDFLAGS: -framework Cocoa
#include <Cocoa/Cocoa.h>
#include "[headerFile].h"
*/
import "C"
import (
"encoding/json"
"sync"
"unsafe"
)
... 使用C库与本地代码交互的公共函数
带有[]
注释的名称表示它们是实际名称的占位符。
我尝试使用arch -x86_64
运行它,但输出结果相同。
这里有什么问题?我需要添加另一个构建标签吗?
附:我在StackOverflow上找到了关于这个错误的问题,但没有一个是关于编译amd64的。这个问题最接近,但它是特定于WebAssembly的。
以下是go env
的结果:
GO111MODULE=""
GOARCH="arm64"
GOBIN=""
GOCACHE="/Users/fperson/Library/Caches/go-build"
GOENV="/Users/fperson/Library/Application Support/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="arm64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/fperson/.asdf/installs/golang/1.16.4/packages/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/fperson/.asdf/installs/golang/1.16.4/packages"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/Users/fperson/.asdf/installs/golang/1.16.4/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/Users/fperson/.asdf/installs/golang/1.16.4/go/pkg/tool/darwin_arm64"
GOVCS=""
GOVERSION="go1.16.4"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/Users/fperson/workspace/personal_projects/timeow/timeow-mac/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 -arch arm64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/mh/02gmkb756x15018g919jcg980000gn/T/go-build4031963624=/tmp/go-build -gno-record-gcc-switches -fno-common"
英文:
My app compiles fine when GOARCH
is set to arm64
(or is omitted). However, when I try to compile an amd64 binary (GOOS=darwin GOARCH=amd64 go build
), I get the following error:
package [mainPackage]
imports [internalPackageThatUsesC]: build constraints exclude all Go files in [pathToInternalPackageThatUsesC]
[internalPackageThatUsesC]
consists of 3 files – an Objective C file with the implementation, a header for it and a Go file which provides an interface for the native code. Following is the Go file:
// +build darwin
package [internalPackageThatUsesC]
/*
#cgo CFLAGS: -x objective-c
#cgo LDFLAGS: -framework Cocoa
#include <Cocoa/Cocoa.h>
#include "[headerFile].h"
*/
import "C"
import (
"encoding/json"
"sync"
"unsafe"
)
... public functions that use the C library to interact with the native code
Names annotated with []
mean they are a placeholder for the actual name.
I tried to run it with arch -x86_64
but the output is the same.
What's the problem here? Do I need to add another build tag?
P.S. I found questions on StackOverflow about this error but none of them were about compiling for amd64. This question was the closest but it was WebAssembly-specific.
Following is the result of go env
:
GO111MODULE=""
GOARCH="arm64"
GOBIN=""
GOCACHE="/Users/fperson/Library/Caches/go-build"
GOENV="/Users/fperson/Library/Application Support/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="arm64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/fperson/.asdf/installs/golang/1.16.4/packages/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/fperson/.asdf/installs/golang/1.16.4/packages"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/Users/fperson/.asdf/installs/golang/1.16.4/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/Users/fperson/.asdf/installs/golang/1.16.4/go/pkg/tool/darwin_arm64"
GOVCS=""
GOVERSION="go1.16.4"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/Users/fperson/workspace/personal_projects/timeow/timeow-mac/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 -arch arm64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/mh/02gmkb756x15018g919jcg980000gn/T/go-build4031963624=/tmp/go-build -gno-record-gcc-switches -fno-common"
答案1
得分: 7
在我的情况下,我还需要将CGO_ENABLED
设置为true。使用GOOS=darwin GOARCH=amd64 CGO_ENABLED=1 go build
命令,应用程序现在可以成功编译为amd64架构。
(感谢这个答案)
英文:
In my case, I also needed to set CGO_ENABLED
to true. The app now compiles fine for amd64 with GOOS=darwin GOARCH=amd64 CGO_ENABLED=1 go build
(Thanks to this answer)
答案2
得分: 1
这个问题的答案(你发布的)谈到了cgo。cgo使用平台特定的头文件/库(在Mac上还包括框架)来调用平台编译器。当你使用CC
进行交叉编译时,你还需要交叉编译的编译器、头文件/库和框架。这并不容易:你可能需要像xgo这样的工具。但是交叉编译可能仍然会失败。
Go是不同的,Go在每个操作系统/架构上使用Go或Plan9汇编重新实现了一个HAL。因此,当你在arm64上交叉编译cgo + go到am64时,go build
将尝试将"cgo+arm64"与"go+amd64"混合在一起。可惜的是,对于内置的go build
工具来说,这是一个空集。
请参考@fperson的答案。
英文:
The answer to the wasm question (as you posted) talks about cgo. cgo invokes platform compiler with platform specific headers/libs (on Mac, with framework too). When you cross-compile with CC
, you also need cross-compile compiler + headers/libs + frameworks. It is not easy: you may need tools like xgo. But still cross-compile may fail.
Go is different, Go re-implements a HAL in go or plan9 ASM on each OS/arch. So when you cross-compile cgo + go for am64 on arm64 together, <strike>go build
will try to blend "cgo+arm64" with "go+amd64". Sadly, it is an empty set for the built-in go build
tool.</strike>
Refer to the @fperson's own answer.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论