Go/Golang从Mac交叉编译到Windows:致命错误:未找到’windows.h’文件

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

Go/Golang Cross-Compile from Mac to Windows: fatal error: 'windows.h' file not found

问题

当我尝试从Mac主机以Windows AMD64为目标交叉编译一个包含C文件的.go源文件时,我遇到了以下错误:

/usr/local/go/src/runtime/cgo/gcc_windows_amd64.c:8:10: fatal error: 'windows.h' file not found

纯粹的Go代码似乎可以无错误地进行交叉编译;在涉及C文件的情况下,是否有办法获取适用于交叉编译的正确头文件?

更多细节:我在我的Mac上安装了LiteIDE,用于处理一些.go项目,而LiteIDE使得将其他平台作为构建目标相对简单。我在一个小的测试项目上进行了测试,纯粹是Go代码,似乎可以正常运行。

后来,我尝试在一个当前的、较大的项目上进行测试,并且不得不调整IDE中的几个环境设置才能使其工作(关于未启用CGO的C文件的问题,以及GOPATH未正确设置,尽管它在.bash_profile中设置了,并且在echo $VARIABLE中进行了验证)。结果是:

/usr/local/go/src/runtime/cgo/gcc_windows_amd64.c:8:10: fatal error: 'windows.h' file not found

尝试将目标设置为Linux(操作系统为linux,架构为amd64)会出现以下错误:

# runtime/cgo
ld: unknown option: --build-id=none
clang: error: linker command failed with exit code 1 (use -v to see invocation)

我已经再次检查了我是否安装了XCode;gcc已经安装:

Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr with-gxx-include-     dir=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/usr/include/c++/4.2.1
Apple LLVM version 7.3.0 (clang-703.0.29)
Target: x86_64-apple-darwin15.4.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin

...而且Go是最新版本:

go version go1.6.2 darwin/amd64

我还检查了这是否只是LiteIDE的问题(因为LiteIDE似乎会覆盖环境设置并忽略终端中的设置?);在终端中尝试的一个示例命令如下:

MyUsername$ env GOOS=windows GOARCH=amd64 CGO_ENABLED=1 go build -v
golang.org/x/net/html/atom
runtime/cgo
golang.org/x/crypto/ssh/terminal
golang.org/x/net/html
github.com/howeyc/gopass
# runtime/cgo
/usr/local/go/src/runtime/cgo/gcc_windows_amd64.c:8:10: fatal error: 'windows.h' file not found
github.com/andybalholm/cascadia
github.com/PuerkitoBio/goquery

我怀疑这是因为应用程序在Go中使用了网络库,而我认为本地库仍然在调用一些C文件来填补空白。是否有办法获取在Linux/Windows上构建所需的正确库,或者是否需要在目标平台上进行这些操作才能正常工作?

在主机平台上构建本机应用似乎没有问题。

英文:

Summary: when I try cross-compiling a .go source file that includes a C file somewhere in the file chain, targeting Windows AMD64 from a Mac host, I get:

/usr/local/go/src/runtime/cgo/gcc_windows_amd64.c:8:10: fatal error: 'windows.h' file not found

Purely Go code seems to cross compile without error; is there a way to get the proper header files for cross compilation when C files are involved?

More details: I installed LiteIDE on my Mac for working on some .go projects, and LiteIDE makes it relatively simple to target other platforms as build targets. I tested it on a small test project I had, purely Go, and it seemed to run without error.

Later I tried it on a current, larger project and had to adjust several env settings in the IDE to get it to work (complaints about C files without CGO enabled, and GOPATH not set properly even though it's set in .bash_profile and verified in echo $VARIABLE just fine.) The result is

/usr/local/go/src/runtime/cgo/gcc_windows_amd64.c:8:10: fatal error: 'windows.h' file not found

Trying to target Linux (os linux, arch amd64) gives

# runtime/cgo
ld: unknown option: --build-id=none
clang: error: linker command failed with exit code 1 (use -v to see invocation)

I've double checked I have XCode installed; gcc is installed:

Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr with-gxx-include-     dir=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/usr/include/c++/4.2.1
Apple LLVM version 7.3.0 (clang-703.0.29)
Target: x86_64-apple-darwin15.4.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin

...and Go is the latest version:

go version go1.6.2 darwin/amd64

I also checked that this isn't just from LiteIDE (since LiteIDE seems to override env settings and ignore what's in the terminal?); an example attempt at the console gives:

MyUsername$ env GOOS=windows GOARCH=amd64 CGO_ENABLED=1 go build -v
golang.org/x/net/html/atom
runtime/cgo
golang.org/x/crypto/ssh/terminal
golang.org/x/net/html
github.com/howeyc/gopass
# runtime/cgo
/usr/local/go/src/runtime/cgo/gcc_windows_amd64.c:8:10: fatal error: 'windows.h' file not found
github.com/andybalholm/cascadia
github.com/PuerkitoBio/goquery

I suspect this is because the application uses networking libraries in Go and I think the native libraries are still calling some C files to fill in gaps. Is there a way to get the proper libraries for building on Linux/Windows or does this need to be done on the target platforms in order to work?

Building native applications on the host platform seems to work without issue.

答案1

得分: 15

要启用CGO的交叉编译,您需要拥有一个可以为目标平台编译C代码的本地工具链。

我对Mac OS X不太熟悉,但在Arch Linux上,我只需要安装mingw-w64-toolchain并使用以下命令编译我的Go代码:

env GOOS="windows" GOARCH="386"   CGO_ENABLED="1" CC="i686-w64-mingw32-gcc"   go build
// 或者目标为64位Windows
env GOOS="windows" GOARCH="amd64" CGO_ENABLED="1" CC="x86_64-w64-mingw32-gcc" go build

在OSX上,您可以使用Homebrew安装mingw:brew install mingw-w64

至于另一个错误消息ld: unknown option: --build-id=none,似乎是一个错误,您可能希望在Go的问题跟踪器上报告该问题。

英文:

To enable cross-compiling for CGO you need to have a local toolchain that can compile C code for that target.

I'm not very familiar with Mac OS X, but on Arch Linux all I had to do was install mingw-w64-toolchain and compile my go code with:

env GOOS="windows" GOARCH="386"   CGO_ENABLED="1" CC="i686-w64-mingw32-gcc"   go build
// or to target win 64
env GOOS="windows" GOARCH="amd64" CGO_ENABLED="1" CC="x86_64-w64-mingw32-gcc" go build

On OSX, you can install mingw with homebrew: brew install mingw-w64

About the other error message though, ld: unknown option: --build-id=none seems like a bug, you might want to report that on the Go issue tracker.

huangapple
  • 本文由 发表于 2016年4月28日 20:46:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/36915134.html
匿名

发表评论

匿名网友

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

确定