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

Bar*_*rim 0 cross-platform header-files go

简介:当我尝试交叉编译包含文件链中某个C文件的.go源文件,从Mac主机定位Windows AMD64时,我得到:

/usr/local/go/src/runtime/cgo/gcc_windows_amd64.c:8:10: fatal error: 'windows.h' file not found
Run Code Online (Sandbox Code Playgroud)

Pure Go代码似乎没有错误地交叉编译; 有没有办法在涉及C文件时获取正确的头文件以进行交叉编译?

更多细节:我在Mac上安装了LiteIDE,用于处理一些.go项目,LiteIDE使得将其他平台作为构建目标的目标相对简单.我在一个小测试项目上测试了它,纯粹是Go,它似乎运行没有错误.

后来我在一个当前更大的项目上尝试了它,并且必须调整IDE中的几个env设置才能使它工作(对没有启用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
Run Code Online (Sandbox Code Playgroud)

试图针对Linux(os linux,arch amd64)给出

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

我已经仔细检查过我安装了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
Run Code Online (Sandbox Code Playgroud)

...和Go是最新版本:

go version go1.6.2 darwin/amd64
Run Code Online (Sandbox Code Playgroud)

我还检查过这不仅仅来自LiteIDE(因为LiteIDE似乎覆盖了env设置而忽略了终端中的内容?); 在控制台上的示例尝试给出:

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
Run Code Online (Sandbox Code Playgroud)

我怀疑这是因为应用程序在Go中使用了网络库,我认为本机库仍在调用一些C文件来填补空白.有没有办法获得适合在Linux/Windows上构建的库,还是需要在目标平台上完成才能工作?

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

One*_*One 8

要为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
// or to target win 64
env GOOS="windows" GOARCH="amd64" CGO_ENABLED="1" CC="x86_64-w64-mingw32-gcc" go build
Run Code Online (Sandbox Code Playgroud)

所以你可能想看看如何在OS X上获得mingw(或类似的东西).

但是,关于其他错误消息,brew install mingw-w64似乎是一个错误,您可能希望在Go 问题跟踪器上报告该错误消息.