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

huangapple go评论90阅读模式
英文:

Golang: How to cross compile on Linux for Windows

问题

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

英文:

How do I cross compile a Go project on Linux to generate an executable for running on Windows?

答案1

得分: 66

要从Linux构建到Windows,您需要设置环境变量GOOSWindows,并将GOARCH设置为amd64

在Bash或ZSH中:

% GOOS=windows GOARCH=amd64 go build
  • 更多详细信息请参阅:https://dave.cheney.net/2015/08/22/cross-compilation-with-go-1-5
  • 关于GOOS和GOARCH可能的值的说明,请参阅:https://golang.org/doc/install/source#environment

如果您的软件包需要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
英文:

To build from Linux to Windows, you need to set the environment variables GOOS to Windows and GOARCH to amd64.

On Bash or ZSH:

% GOOS=windows GOARCH=amd64 go build

If your package requires CGO then you need to use the mingw-w64 compiler:

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

答案2

得分: 7

简介

在生产过程中,您可能希望构建支持 Windows 架构的 Go 程序,但这并不总是容易的,下面是一个指南,帮助您为 Windows 构建 Go 程序。

CGO

Cgo 是 Go 的一个元素,允许 Go 包调用 C 代码。

但是,是否使用 CGO 并不像“我是否调用了 C 代码”那么简单,因为虽然您可能没有在 Go 程序中调用 C 代码,但您使用的某个包可能会调用。

要弄清楚这一点,最简单的方法是先尝试不使用 CGO,如果不起作用再尝试第二个选项。(但如果您愿意,也可以尝试这个

如果您不需要 CGO

如果您不需要 CGO,将您的 Go 程序构建到另一个平台非常简单,因为 Go 本身支持跨平台编译。
您只需要执行以下操作:

x64

# 将您的 Go 程序编译到 Windows x64 平台
env GOOS=windows GOARCH=amd64 go build package-import-path

x32

# 将您的 Go 程序编译到 Windows x32 平台
env GOOS=windows GOARCH=386 go build package-import-path

提示:由于 Go 本身支持交叉编译,您还可以轻松地将程序构建到其他平台,更多信息请参阅这里

如果您需要 CGO

如果您需要 CGO,构建您的程序会稍微复杂一些,因为 C 不支持本地跨平台编译。但是不用担心!使用MinGW64项目仍然非常简单。

要求

由于我们将使用 mingw64 来构建项目,我们需要确保 mingw 已安装。如果没有安装,可以按照以下步骤安装:

Ubuntu

在 Ubuntu 上运行以下命令:

sudo apt-get install gcc-mingw-w64

Fedora

在 Fedora 上运行以下命令:

sudo dnf install mingw64-gcc

构建

现在我们满足了要求,可以开始构建我们的项目了。

对于 Windows x64 架构

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

对于 Windows x32 架构

要在 Windows x32 架构上构建您的程序,请运行:

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

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

CGO

Cgo is an element of go that allow Go packages to call C code.

But whether you're using CGO or not is not as easy as "did I called C code?" because while you may not have called C code in you Go program, a package that you're using probably does.

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

if you don't need CGO, building your go program to another platform is pretty easy as go natively support cross-platform compiling.
all you need to do is this:

x64

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

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 easily build your program to other platform as well, further reading here

If you need 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 worry! It is still pretty easy using the MinGW64 project.

Requirements

Since we will be using mingw64 to build our project, we'll need to make sure mingw is installed.
If it's not here's how you can install it:

Ubuntu

On ubuntu simply run:

sudo apt-get install gcc-mingw-w64

Fedora

On fedora simply run:

sudo dnf install mingw64-gcc

Build

Now that we met the requirements, we can now build our project

For windows x64 architecture

To build your program on a windows x64 architecture run:

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

To build your program on a windows x32 architecture run:

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

huangapple
  • 本文由 发表于 2017年1月10日 18:22:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/41566495.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定