交叉编译 Go 语言

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

Cross Compiling Go

问题

我正在尝试在我的MacBook上为Ubuntu Linux x86_64交叉编译Go语言。我按照这里的说明进行操作,但是当我运行go-linux-amd64 build时,我收到以下消息:go build runtime: linux/amd64 must be bootstrapped using make.bash。希望能得到帮助。

英文:

I am trying to cross compile Go for ubuntu linux x86_64 on my macbook. I have followed instructions outlined here but when I run go-linux-amd64 build I get the following message go build runtime: linux/amd64 must be bootstrapped using make.bash. Any help with this will be appreciated.

答案1

得分: 31

它说你需要重新构建适用于linux-amd64的库和运行时。你可以按照以下步骤进行操作:

  1. 找到你的Go安装的根目录(如果你不知道在哪里,运行which go可能会有所帮助 - 该二进制文件通常与其他源代码一起安装)。
  2. 进入src目录。
  3. 运行GOOS=linux GOARCH=amd64 ./make.bash --no-clean(如果make.bash不可执行,可以运行GOOS=linux GOARCH=amd64 bash make.bash --no-clean)。这将使用指定的操作系统和架构重新构建库和运行时。

完成这些步骤后,你可以使用GOOS=linux GOARCH=amd64 go build为该架构构建Go包或二进制文件。你可以按照相同的说明为其他架构和操作系统进行操作。

编辑(08/13/15):

从Go 1.5开始,交叉编译变得更加容易。由于运行时是用Go编写的,所以无需设置任何内容即可进行交叉编译。现在你只需从标准的Go安装中运行GOOS=<os> GOARCH=<arch> go build即可。

然而,这里有一个例外情况。如果你正在使用cgo,仍然需要提前设置一些东西。并且你需要通过将CGO_ENABLED环境变量设置为1来通知工具,你想要启用cgo交叉编译。因此,具体步骤如下:

  1. 进入你的Go安装的src目录(参见上面的说明)。
  2. 运行CGO_ENABLED=1 GOOS=<os> GOARCH=<arch> ./make.bash --no-clean
  3. 运行CGO_ENABLED=1 go build来构建你的项目。即使在编译时,你也需要指定CGO_ENABLED=1
英文:

What it's saying you need to do is rebuild the library and runtime for linux-amd64. You can do that this way:

  1. Find the root of your Go installation (if you don't know where this is, running which go may help - the binary is often installed with the rest of the sources).
  2. cd into the src directory
  3. Run GOOS=linux GOARCH=amd64 ./make.bash --no-clean (or GOOS=linux GOARCH=amd64 bash make.bash --no-clean if make.bash is not executable). This will rebuild the library and runtime using the specified OS and architecture.

Once you've done this, you can build a go package or binary for this architecture using GOOS=linux GOARCH=amd64 go build. You can follow the same instructions for other architectures and operating systems.

Edit (08/13/15):

As of Go 1.5, cross compiling is much easier. Since the runtime is written in Go, there's no need to set anything up in order to be able to cross-compile. You can now just run GOOS=&lt;os&gt; GOARCH=&lt;arch&gt; go build from a vanilla Go installation and it will work.

However, there's one exception to this. If you're using cgo, you'll still need to set stuff up ahead of time. And you'll need to inform the tooling that you want to enable cgo cross-compiling by setting the CGO_ENABLED environment variable to 1. So, to be precise:

  1. cd into the src directory of your Go installation (see the instructions above).
  2. Run CGO_ENABLED=1 GOOS=&lt;os&gt; GOARCH=&lt;arch&gt; ./make.bash --no-clean
  3. Run CGO_ENABLED=1 go build to build your project. It is important that you specify CGO_ENABLED=1 even when you're compiling.

答案2

得分: 1

根据上面的答案https://stackoverflow.com/a/27413148/3675575,我需要设置GOROOT_BOOTSTRAP来重新编译我的GO源代码树:

GOROOT_BOOTSTRAP=/usr/lib/golang/ CGO_ENABLED=1 GOOS=linux GOARCH=386 ./make.bash --no-clean

(我正在使用Fedora 23,所以在您的操作系统中,GOROOT_BOOTSTRAP可能会有所不同)

英文:

Following the above answer https://stackoverflow.com/a/27413148/3675575, I needed to set GOROOT_BOOTSTRAP to recompile my GO source tree:

GOROOT_BOOTSTRAP=/usr/lib/golang/ CGO_ENABLED=1 GOOS=linux GOARCH=386 ./make.bash --no-clean

(I'm using Fedora 23, so the GOROOT_BOOTSTRAP may vary in your operating system)

答案3

得分: -1

你必须执行 cd %goroot%/src/,找到 make.bash

然后执行 ./make.bash

执行你的命令。试试看!

英文:

You must cd %goroot%/src/,find make.bash

Then execute ./make.bash

execute your command. Try it!

huangapple
  • 本文由 发表于 2014年12月11日 07:12:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/27412601.html
匿名

发表评论

匿名网友

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

确定