如何从Windows交叉编译到Linux?

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

How to cross compile from Windows to Linux?

问题

我在Windows机器上安装了Go 1.2,并编写了一个虚拟程序,并将环境变量GOARCHGOOS分别设置为"AMD64"和"linux"。

当我执行"go build"命令时,我收到了一个错误信息:

go build runtime: linux/amd64 must be bootstrapped using make.bat

这是什么意思?

英文:

I've installed Go 1.2 on a Windows machine, wrote up a dummy program and set the environment variables GOARCH and GOOS to "AMD64" and "linux" respectively.

When i issue the "go build" command, i receive an error:

go build runtime: linux/amd64 must be bootstrapped using make.bat

What does this mean?

答案1

得分: 57

它告诉你在使用这些工具之前需要先构建它们。

如果你的 Windows GOARCH 是 amd64,那么你可以通过运行以下小批处理程序来“构建”所有所需的工具:

set GOARCH=amd64
set GOOS=linux
go tool dist install -v pkg/runtime
go install -v -a std

如果成功了,那么你应该能够做你所描述的事情(只需使用 amd64,而不是 AMD64 - 它是区分大小写的)。

如果你的 Windows GOARCH 是 386,那么你需要先构建你的 386 工具。你需要下载 mingw gcc。按照 user2714852 所说的做。

在这里 https://golang.org/wiki/WindowsCrossCompiling 有类似的针对 Linux 的说明,也许对你有帮助。

Alex

英文:

It tells you it needs all tools built before you can use them.

If your windows GOARCH is amd64, then you could "build" all required tools by running this small batch programs:

set GOARCH=amd64
set GOOS=linux
go tool dist install -v pkg/runtime
go install -v -a std

If that succeeds then you should be able to do what you've described (just use amd64, not AMD64 - it is case sensitive).

If your windows GOARCH is 386, then you would need to build your 386 tools first. You would need to download mingw gcc for that. Do what user2714852 said.

Here https://golang.org/wiki/WindowsCrossCompiling are similar instructions for linux, perhaps you find them helpful.

Alex

答案2

得分: 38

我在从Windows构建Linux时遇到了一些严重的问题,最后发现解决方法相当简单。我想评论Alex的帖子,但由于我是Stack Overflow的新手,所以无法评论。

正如Alex所说,设置环境变量。这必须以管理员身份执行(例如,右键单击“命令提示符”或“PowerShell”快捷方式,然后单击“以管理员身份运行”)

set GOARCH=amd64
set GOOS=linux

如果你使用PowerShell,使用以下命令:

$Env:GOOS = "linux"; $Env:GOARCH = "amd64"

如果你不以管理员身份执行此操作,变量将不会生效,你只会在当前操作系统和架构上进行构建。

我发现通过运行go env来检查你的go环境变量总是很好,它会给你当前go环境变量的列表

go env
set GOARCH=amd64
set GOBIN=
set GOEXE=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOOS=linux
set GOPATH=T:\Projects\gopath
set GORACE=
set GOROOT=C:\Go
set GOTOOLDIR=C:\Go\pkg\tool\windows_amd64
set GCCGO=gccgo
set CC=gcc
set GOGCCFLAGS=-fPIC -m64 -fmessage-length=0
set CXX=g++
set CGO_ENABLED=0
set PKG_CONFIG=pkg-config
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2

确保GOOS和GOARCH的值与你之前指定的值相同。

如果一切正常,你应该能够导航到包含你的Go代码的目录,并从命令行运行:

go build

这将根据环境变量中设置的系统和架构构建包。我在最后弄清楚这一点后遇到了一些其他问题,但那是另一个与此问题无关的问题。

英文:

I was having some major problems with building for linux from windows, At the end of the day, it was fairly simple. I would comment on Alex's post, but I can not as I am a stackoverflow newb.

As alex said, set the environment variables. This must be done as administrator (eg right click the "command prompt" or "Powershell" shortcut and click "Run as Administrator")

set GOARCH=amd64
set GOOS=linux

If you use Powershell, use

$Env:GOOS = "linux"; $Env:GOARCH = "amd64"

If you dont do it as administrator, the variables wont take effect and you will just be building it for the OS & Architecture you are on at the moment.

I found its always good to check your go environment vars by running go env, which gives you the list of current go environment variables

go env
set GOARCH=amd64
set GOBIN=
set GOEXE=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOOS=linux
set GOPATH=T:\Projects\gopath
set GORACE=
set GOROOT=C:\Go
set GOTOOLDIR=C:\Go\pkg\tool\windows_amd64
set GCCGO=gccgo
set CC=gcc
set GOGCCFLAGS=-fPIC -m64 -fmessage-length=0
set CXX=g++
set CGO_ENABLED=0
set PKG_CONFIG=pkg-config
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2

Make sure the GOOS & GOARCH are set to the values you specified earlier.

If thats all good, you should be able to navigate to the directory containing your go code, and from the command line run:

go build

Which will build the package for the system and the architecure that are set in the environment variables.
I encountered some other issues once I finally figured this out, but that is another matter not related to this issue.

答案3

得分: 22

要设置PowerShell环境变量,请使用以下命令(无需管理员权限):
$env:GOOS = "linux"

然后构建你的程序:
go build

更改的环境变量仅在当前的PowerShell窗口中有效。当你重新打开窗口时,所有设置将被重置。

英文:

To set the PowerShell environment variables use (no admin mode required):
$env:GOOS = "linux"
Than build your programm go build

The changed environment variable is only pesent in the current PowerShell window. Everything will be resetted when you reopen the window.

答案4

得分: 1

交叉编译Go语言,首先你需要能够从源代码构建Go语言。为了做到这一点,看起来你需要安装MinGW以获取gcc和其他工具。关于这方面的帮助可以在https://code.google.com/p/go-wiki/wiki/WindowsBuild找到。

从那里开始,如果它类似于Linux的交叉编译,可以按照以下步骤进行:

首先,使用cd命令进入你的go\src目录。如果你不确定它在哪里,可以输入go env命令,你会在输出中看到一行类似于GOROOT="\some\dir\"的内容;只需执行cd \some\dir\src\命令即可。

然后,设置GOOS=linuxGOARCH=amd64,然后输入.\make.bat命令,这将构建一个针对Linux目标的Go编译器版本等。然后你就不应该再遇到这个错误了。

英文:

To cross-compile Go, fist you need to be able to build Go from the source code. To do that, it looks like you need to install MinGW to get gcc and other tools. Help on that is at https://code.google.com/p/go-wiki/wiki/WindowsBuild.

From there, here's how it goes if it's like Linux cross-compiling:

First cd to your your go\src directory. If you're not sure where that is, type go env and you'll see a line like GOROOT="\some\dir\" in the output; just do cd \some\dir\src\

Then, with GOOS=linux and GOARCH=amd64 set, type .\make.bat, which will build a version of the Go compiler, etc. targeting Linux. Then you shouldn't get this error anymore.

答案5

得分: 1

如果你使用docker,你可以为linux构建一个docker镜像。例如:

首先,你可以准备一个如下所示的DockerFile

FROM golang:1.15-alpine3.12 as builder

RUN mkdir /go/src/hello

WORKDIR /go/src/hello

#安装nano、zip和git
RUN apk add nano zip git

COPY ./ ./

#构建main
RUN GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -o main

#压缩main
RUN zip main.zip main

然后你可以使用docker build -t yourname/hello .命令构建一个镜像。之后需要从这个镜像中导出你的zip文件:

  • 首先,使用命令docker run -it yourname/hello将镜像作为容器运行。
  • 其次,打开一个新的终端并运行docker ps命令查看容器ID。
  • 最后,运行命令docker cp YOUR_WORKING_CONTAINER_ID:/go/src/hello/main.zip .导出zip文件。

这可能是一个比较繁琐的过程,但它将允许你在使用docker时不依赖于平台。下面是我为使用docker镜像更新golang lambda函数(通过aws-cli)开发的一个示例。

英文:

If you using docker, you can build a docker image for linux builts. For example:

First of all, you can prepare a DockerFile as follows:

FROM golang:1.15-alpine3.12 as builder

RUN mkdir /go/src/hello

WORKDIR /go/src/hello

#install nano, zip and git
RUN apk add nano zip git

COPY ./ ./

#build main
RUN GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -o main

#zip main
RUN zip main.zip main

Than you can build an image with docker build -t yourname/hello . After that have to export your zip file from this image:

  • First, use the image as a container with the command docker run -it yourname / hello.
  • Second, open a new terminal and run the docker ps to view the Container IDs.
  • And finally, run the command docker cp YOUR_WORKING_CONTAINER_ID:/go/src/hello/main.zip . to export the zip file.

This can be a long way, but it will allow you to build using docker without being dependent on the platform. Below is an example developed by me for using a docker image for updating golang lambda function (via aws-cli).

huangapple
  • 本文由 发表于 2013年12月30日 03:22:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/20829155.html
匿名

发表评论

匿名网友

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

确定