如何从Windows交叉编译到Linux?

我已经在Windows计算机上安装了Go 1.2,编写了一个虚拟程序并设置了环境变量 GOARCH </ code>和 GOOS </ code>分别为“ AMD64”和“ linux”。</ p>

当我发出“ go build </ code>”时 命令,我收到一个错误:</ p>

 开始构建运行时:必须使用make.bat 
</ code> </ pre>

<启动Linux / amd64 p>这是什么意思?</ p>
</ div>

展开原文

原文

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?

4个回答



它告诉您需要先构建所有工具,然后才能使用它们。</ p>

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

  set GOARCH = amd64 
set GOOS = linux
go工具dist install -v pkg / runtime
go install -v -a std
</ code> </ pre>

如果成功,则您应该能够执行所描述的操作(仅使用amd64,而不是AMD64 -区分大小写。)</ p>

如果您的Windows GOARCH是386,则需要首先构建386工具。 您需要为此下载mingw gcc。 执行用户2714852所说的话。</ p>

在这里 https://golang.org/wiki/WindowsCrossCompiling 是与Linux类似的说明,也许您会发现它们有用。</ p>

Alex </ p>
</ div>

展开原文

原文

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

douzhan8303
douzhan8303 感谢您的澄清。 我已经安装了1.11.4,并在运行该命令时收到错误,但最终却没有了。
一年多之前 回复
dongyangben6144
dongyangben6144 在非常旧的Go版本中,该命令是必需的。 如果使用go1.12,则只需设置GOARCH和GOOS并运行'go install'或'go build'命令。
一年多之前 回复
donglu4633
donglu4633 go工具dist install -v pkg / runtime有什么作用?
一年多之前 回复
duanjuete9206
duanjuete9206 我正在使用go 1.7.5,似乎只有设置GOOS = linux才能构建linux版本。
接近 3 年之前 回复



要设置PowerShell环境变量,请使用(不需要管理模式):

$ env:GOOS =“ linux” </ code>

比构建程序m go build </ code> </ p>

更改的环境变量仅在当前PowerShell窗口中存在。 重新打开窗口后,所有内容都会重置。</ p>
</ div>

展开原文

原文

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.



要交叉编译Go,首先需要能够从源代码构建Go。 为此,您似乎需要安装MinGW才能获取gcc和其他工具。 有关帮助,请访问 https://code.google.com/p/go- wiki / wiki / WindowsBuild 。</ p>

从那里开始,就像Linux交叉编译一样:</ p>

第一个 cd </ code>到您的 go \ src </ code>目录。 如果不确定该在哪里,请键入 go env </ code>,然后在输出中会看到类似 GOROOT =“ \ some \ dir \” </ code>的行; 只需 cd \ some \ dir \ src \ </ code> </ p>

然后用 GOOS = linux </ code>和 GOARCH = amd64 </ 设置代码,然后键入。\ make.bat </ code>,这将构建针对Linux的Go编译器等版本。 那么您就不会再收到此错误。</ p>
</ div>

展开原文

原文

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.

dongmei8511
dongmei8511 在运行make.bat之前,您需要从MinGW下载并安装gcc编译器。 我已经为此添加了说明。
接近 7 年之前 回复
doufei7516
doufei7516 这是一个批处理文件(从过去想起AUTOEXEC.bat),其中包含在系统上构建Go的命令。
接近 7 年之前 回复
douzhangbao2187
douzhangbao2187 什么是“ make.bat”?
接近 7 年之前 回复

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

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐