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

时间:2016-04-28 12:46:03

标签: go cross-platform header-files

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

/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中的几个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

尝试针对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)

我已经仔细检查过安装了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似乎覆盖了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

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

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

1 个答案:

答案 0 :(得分:7)

要为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

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

关于其他错误消息,ld: unknown option: --build-id=none似乎是一个错误,您可能希望在Go ui-router state parameters跟踪器上报告该内容。