mg03
mg03

Reputation: 767

golang cannot execute binary file: 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

Upvotes: 37

Views: 100698

Answers (5)

Beolap
Beolap

Reputation: 938

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: Go-compiled binary won't run in an alpine docker container on Ubuntu host

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

Upvotes: 1

fafrd
fafrd

Reputation: 1136

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/

Upvotes: 4

DMin
DMin

Reputation: 10353

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.

Upvotes: 0

Alex Dvoretskiy
Alex Dvoretskiy

Reputation: 347

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.

Upvotes: 14

WeakPointer
WeakPointer

Reputation: 3302

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.

Upvotes: 60

Related Questions