golang无法执行二进制文件:执行格式错误

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

golang cannot execute binary file: Exec format error

问题

我的go环境

GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH=""
GORACE=""
GOROOT="/usr/local/go"
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GO15VENDOREXPERIMENT=""
CC="gcc"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0"
CXX="g++"
CGO_ENABLED="1"

我在我的Mac上编译并成功执行了二进制文件,然后将其复制到Ubuntu机器上,该机器的go env如上所示。当我调用myprog二进制文件时,我得到以下错误信息:

bash: /usr/local/go/bin/myprog: 无法执行二进制文件: Exec format error
英文:

my go env

GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH=""
GORACE=""
GOROOT="/usr/local/go"
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GO15VENDOREXPERIMENT=""
CC="gcc"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0"
CXX="g++"
CGO_ENABLED="1"

I compiled and successfully executed the binary on my mac and then copied it to the ubuntu machine whose go env is show above. When I call myprog binary, I get

bash: /usr/local/go/bin/myprog: cannot execute binary file: Exec format error

答案1

得分: 53

自从Go 1.5版本以来,交叉编译变得非常简单。类似这样的命令:

env GOOS=linux GOARCH=amd64 go build -v github.com/constabulary/gb/cmd/gb

请参考http://dave.cheney.net/2015/08/22/cross-compilation-with-go-1-5。

英文:

Since go 1.5, cross compiler has gotten pretty easy. Something like

env GOOS=linux GOARCH=amd64 go build -v github.com/constabulary/gb/cmd/gb

Refer to http://dave.cheney.net/2015/08/22/cross-compilation-with-go-1-5.

答案2

得分: 12

我遇到了同样的问题。我安装了64位版本的go而不是32位版本。在安装32位版本之后,它正常工作了。

英文:

I had the same problem. I installed 64-bit version of go instead of 32-bit version. After installation of 32-bit version it works fine.

答案3

得分: 2

这是我在下载 golang 的 x86 版本时遇到的问题,但我提供的虚拟机是 ARM 架构的 CPU。

你可以在这里获取 ARM 版本的下载链接:https://go.dev/dl/

英文:

This happened to me when I was downloading the x86 version of golang, but the VM i provisioned was an ARM cpu.

You can get ARM downloads here: https://go.dev/dl/

答案4

得分: 1

可能的情况是,您的二进制文件动态链接到一些在您的操作系统上未安装的C库。这可能是因为您在编译和运行二进制文件的环境不同。

要获取有关您的二进制文件的一些信息,您可以运行file ./your-binaryld ./your-binary命令。

我建议您参考以下帖子,其中提供了一些解决该问题的方法:https://stackoverflow.com/q/36279253/9068781

基本上,您有以下几个选择:

  • 在与编译环境相同的架构/操作系统上运行
  • 在目标操作系统上安装缺失的库
  • 尝试进行静态编译
英文:

It may be the case that your binary is dynamically linked to some C-libraries, which are not installed on your OS. This can happen if you e.g. compile in a different environment than where you run your binary.

To get some info about your binary, you can both run file ./your-binary and ld ./your-binary.

I recommend this post for some solutions to that problem: https://stackoverflow.com/q/36279253/9068781

Basically, your options are:

  • run on the same architecture/OS as you compile
  • install the missing libraries on your target OS
  • try to statically compile

答案5

得分: 0

在Windows上,请确保你(或你的IDE)没有运行PowerShell - PowerShell不会接受set GOOS命令,也不会抛出错误,但仍会编译二进制文件。当你将二进制文件部署到服务器时,你会遇到这个错误。

英文:

On windows, make sure you(or your IDE) is not running PowerShell - powershell will not take the set GOOS and not throw an error and will still compile the binary and you'll find this error when you deploy the binary to the server.

huangapple
  • 本文由 发表于 2016年3月24日 18:50:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/36198418.html
匿名

发表评论

匿名网友

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

确定