英文:
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的库和运行时。你可以按照以下步骤进行操作:
- 找到你的Go安装的根目录(如果你不知道在哪里,运行
which go
可能会有所帮助 - 该二进制文件通常与其他源代码一起安装)。 - 进入
src
目录。 - 运行
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交叉编译。因此,具体步骤如下:
- 进入你的Go安装的
src
目录(参见上面的说明)。 - 运行
CGO_ENABLED=1 GOOS=<os> GOARCH=<arch> ./make.bash --no-clean
。 - 运行
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:
- 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). cd
into thesrc
directory- Run
GOOS=linux GOARCH=amd64 ./make.bash --no-clean
(orGOOS=linux GOARCH=amd64 bash make.bash --no-clean
ifmake.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=<os> GOARCH=<arch> 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:
cd
into thesrc
directory of your Go installation (see the instructions above).- Run
CGO_ENABLED=1 GOOS=<os> GOARCH=<arch> ./make.bash --no-clean
- Run
CGO_ENABLED=1 go build
to build your project. It is important that you specifyCGO_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!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论