英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论