Go / Golang交叉编译从Mac到Windows:致命错误:找不到'windows.h'文件 [英] Go/Golang Cross-Compile from Mac to Windows: fatal error: 'windows.h' file not found

查看:4125
本文介绍了Go / Golang交叉编译从Mac到Windows:致命错误:找不到'windows.h'文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

摘要:当我尝试交叉编译在文件链中某处包含C文件的.go源文件时,我从Mac主机中获得了针对Windows AMD64的版本:

  /usr/local/go/src/runtime/cgo/gcc_windows_amd64.c:8:10:致命错误:'windows.h'文件不是发现

Purely Go代码似乎无错地交叉编译;是否有一种方法可以在C文件涉及时交叉编译正确的头文件?



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

后来,我在一个当前更大的项目上尝试了它,不得不调整IDE中的几个env设置以使其运行(关于没有启用CGO的C文件的投诉,即使它设置在.bash_profile中并且在echo $ VARIABLE中验证,GOPATH也没有设置正确)。结果是

  /usr/local/go/src/runtime/cgo/gcc_windows_amd64.c:8:10:致命错误:'windows.h'文件未找到

试图以Linux(os linux,arch amd64)为目标给出

 #runtime / cgo 
ld:未知选项:--build-id = none
clang:error:连接器命令失败,退出代码1(使用-v来查看调用)

我已经仔细检查过了,我安装了XCode; gcc安装完毕:

 配置为:--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 7.3版。 0(clang-703.0.29)
目标:x86_64-apple-darwin15.4.0
线程模型:posix
InstalledDir:/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault。 xctoolchain / usr / bin

...和Go是最新版本:

  go version go1.6.2达尔文/ 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:致命错误:'windows.h'找不到文件
github.com/andybalholm/cascadia
github.com/PuerkitoBio/goquery

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



构建原生应用程序主机平台似乎没有问题。

解决方案

要为CGO交叉编译,您需要有一个本地工具链编译该目标的C代码。



我对Mac OS X不太熟悉,但在Arch Linux上,我只需安装 mingw-w64-toolchain 并编译我的代码:

  env GOOS =windowsGOARCH =386CGO_ENABLED =1CC =i686-w64-mingw32-gccgo build 
// or to win win 64
env GOOS =windowsGOARCH =amd64CGO_ENABLED = 1CC =x86_64-w64-mingw32-gccgo build

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

关于另一个错误消息,虽然, ld:未知选项: - b uild-id = none 看起来像一个bug,你可能想在Go上报告问题跟踪器。


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.

解决方案

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

So you might want to look into how to get mingw (or something similar on OS X.

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.

这篇关于Go / Golang交叉编译从Mac到Windows:致命错误:找不到'windows.h'文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆