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

时间:2021-05-27 15:08:57

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:

总结:当我尝试交叉编译一个。go源文件,其中包含一个C文件在文件链的某个地方,针对Windows AMD64从Mac主机,我得到:

/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?

纯粹的Go代码似乎没有错误地交叉编译;当涉及C文件时,是否有方法获得正确的交叉编译头文件?

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.

更多的细节:我在我的Mac上安装了LiteIDE,用于开发一些。go项目,LiteIDE将其他平台作为构建目标相对简单。我在一个很小的测试项目上测试了它,它完全可以运行,而且它运行起来似乎没有任何错误。

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

后来,我在一个更大的项目上尝试了它,并且不得不在IDE中调整几个env设置以使它能够工作(关于C文件的抱怨没有启用CGO,而GOPATH没有正确设置,即使它设置在.bash_profile中,并在echo $VARIABLE中进行了验证)。结果是

/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

尝试针对Linux(操作系统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)

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

我已经对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

...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:

我还检查了这不仅仅是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

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?

我怀疑这是因为应用程序使用了网络库,我认为本地库仍然调用一些C文件来填补空白。是否有一种方法可以在Linux/Windows上建立合适的库,或者是否需要在目标平台上进行工作?

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

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

1 个解决方案

#1


6  

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

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

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:

我对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

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

因此,您可能需要研究如何获得mingw(或类似于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.

关于另一个错误消息,ld:未知选项:-build-id=none看起来像一个bug,您可能需要在Go问题跟踪器上报告它。

#1


6  

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

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

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:

我对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

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

因此,您可能需要研究如何获得mingw(或类似于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.

关于另一个错误消息,ld:未知选项:-build-id=none看起来像一个bug,您可能需要在Go问题跟踪器上报告它。