简体   繁体   English

Golang:如何在 Linux for Windows 上交叉编译

[英]Golang: How to cross compile on Linux for Windows

如何在 Linux 上交叉编译 Go 项目以生成在 Windows 上运行的可执行文件?

To build from Linux to Windows, you need to set the environment variables GOOS to Windows and GOARCH to amd64 .要从 Linux 构建到 Windows,您需要将环境变量GOOS设置为Windows并将GOARCHamd64

On Bash or ZSH:在 Bash 或 ZSH 上:

% GOOS=windows GOARCH=amd64 go build

If your package requires CGO then you need to use the mingw-w64 compiler:如果你的包需要CGO ,那么你需要使用 mingw-w64 编译器:

sudo apt-get install gcc-multilib
sudo apt-get install gcc-mingw-w64

GOOS=windows GOARCH=386 \
  CGO_ENABLED=1 CXX=i686-w64-mingw32-g++ CC=i686-w64-mingw32-gcc \
  go build

Introduction介绍

During your production process you may want to build your go program to support a windows architecture, but this is not always easy but here's a guide to help you building your go program for windows在您的生产过程中,您可能希望构建您的 go 程序以支持 windows 架构,但这并不总是那么容易,但这里有一个指南可以帮助您构建适用于 windows 的 go 程序

CGO CGO

Cgo is an element of go that allow Go packages to call C code. Cgo 是 go 的一个元素,它允许 Go 包调用 C 代码。

But weather you're using CGO or not is not as easy as "did I called C code?"但是天气你是否使用 CGO 并不像“我调用 C 代码吗?”那么简单。 because while you may didn't have called C code in you Go program, a package that you're using probably does.因为虽然您可能没有在 Go 程序中调用 C 代码,但您正在使用的包可能会调用。

To figure it out the easiest thing is to try without and if it's not working try the second option.要弄清楚最简单的方法是尝试不使用,如果它不起作用,请尝试第二个选项。 (But you can also try this if you want to) (但如果你愿意,你也可以试试这个

If you don't need CGO如果你不需要 CGO

if you don't need CGO, building your go program to another platform is pretty easy as go natively support cross-platform compiling.如果您不需要 CGO,将您的 go 程序构建到另一个平台非常容易,因为 go 原生支持跨平台编译。 all you need to do is this:您需要做的就是:

x64 x64

#Compile your go program to the windows x64 platform 
env GOOS=windows GOARCH=amd64 go build package-import-path

x32 x32

#Compile your go program to the windows x32 platform 
env GOOS=windows GOARCH=386 go build package-import-path

Tip: Since go natively support cross compiling, you can easely build your program to other platform as well, further reading here提示:由于 go 原生支持交叉编译,您也可以轻松地将程序构建到其他平台,进一步阅读这里

If you need CGO如果你需要 CGO

if you need CGO, building your program will be a little bit more complicated since C doesn't support native cross-platform compiling.. But don't wory!如果你需要 CGO,构建你的程序会有点复杂,因为 C 不支持本地跨平台编译。但不要担心! It is still pretty easy using the MinGW64 project.使用MinGW64项目仍然很容易。

Requirements要求

Since we will be using mingw64 to build our project, we'll need to make sure mingw is installed.由于我们将使用 mingw64 来构建我们的项目,因此我们需要确保安装了 mingw。 If it's not here's how you can install it:如果它不是这里是你如何安装它:

Ubuntu Ubuntu

On ubuntu simply run:在 ubuntu 上只需运行:

sudo apt-get install gcc-mingw-w64

Fedora软呢帽

On fedora simply run:在 fedora 上简单地运行:

sudo dnf install mingw64-gcc

Build建造

Now that we met the requirements, we can now build our project现在我们满足了要求,我们现在可以构建我们的项目

For windows x64 architecture适用于 windows x64 架构

To build your program on a windows x64 architecture run:要在 windows x64 架构上构建程序,请运行:

GOOS=windows GOARCH=amd64 CGO_ENABLED=1 CXX=x86_64-w64-mingw32-g++ CC=x86_64-w64-mingw32-gcc go build package-import-path

For windows x32 architecture对于 windows x32 架构

To build your program on a windows x32 architecture run:要在 windows x32 架构上构建程序,请运行:

GOOS=windows GOARCH=386 CGO_ENABLED=1 CXX=i686-w64-mingw32-g++ CC=i686-w64-mingw32-gcc go build package-import-path

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM