如何将我的Go程序从Mac OS X交叉编译到Ubuntu 64位?

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

How do I cross compile my Go program from Mac OS X to Ubuntu 64-bit

问题

如标题所述,我想知道如何交叉编译我的程序,以便在Ubuntu 64位上运行。

我进入了/usr/local/go/src文件夹并运行了以下命令:

GOOS=linux GOARCH=amd64 ./make.bash --no-clean

一切都编译得很好。

然后我进入我的项目目录并运行go build -v -a,然后将编译好的二进制文件移动到我的Linux服务器上,但在运行时出现以下错误:

root@PanicCSGO40:~/test# ./test
-bash: ./test: cannot execute binary file: Exec format error
root@PanicCSGO40:~/test# sudo ./test
./test: 1: ./test: Syntax error: "(" unexpected
root@PanicCSGO40:~/test#

不确定我做错了什么,任何信息都会很有帮助,谢谢。

我还尝试使用GOARCH=386进行操作,但仍然出现相同的错误。谢谢!

这个链接不能解决我的问题,因为所选答案是一个链接到博客文章,该文章在很大程度上依赖于使用博客作者的bash脚本进行所有交叉编译,我只是想知道正确的方法是什么,现在我知道了。

英文:

As the title says I'm wondering how to cross-compile my program so that I can run it on Ubuntu 64-bit

I've went into the /usr/local/go/src folder and ran

GOOS=linux GOARCH=amd64 ./make.bash --no-clean

everything compiled fine

then went into my project directory and ran go build -v -a and then took the compiled binary and moved it to my linux server, but when running it I get this error:

root@PanicCSGO40:~/test# ./test
-bash: ./test: cannot execute binary file: Exec format error
root@PanicCSGO40:~/test# sudo ./test
./test: 1: ./test: Syntax error: "(" unexpected
root@PanicCSGO40:~/test#

Not sure what I am doing wrong any information would be great thanks.

I've also tried doing it with GOARCH=386 but still get the same errors. Thanks!

This link does not solve my question because the chosen answer is a link to a blog post which relies heavily on doing all cross-compilation on using the blog writers bash scripts to do it, I just simply wanted to know what the correct way to do it was and now I do.

答案1

得分: 87

构建命令需要识别目标环境:

$ GOOS=linux GOARCH=amd64 go build -v /path/to/target/package
英文:

The build command needs to identify the target environment:

$ GOOS=linux GOARCH=amd64 go build -v /path/to/target/package

huangapple
  • 本文由 发表于 2015年9月14日 12:45:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/32557438.html
匿名

发表评论

匿名网友

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

确定