Golang cross-compiled program not works on OpenWrt x86_64

Hi,
i want to use cross-compiling to run my go program my Openwrt x86_x64

my work to build go program

root@builderlinux:~# env GOOS=linux GOARCH=amd64 go build test.go

But when i started the program it gives not found error.
Any help would be appreciated.

root@testlinux:~# chmod 777 test
root@testlinux:~# ./test 
-sh: ./test: not found

target system:

SHELL=/bin/sh

root@testlinux:~# uname -a
Linux testlinux 4.14.209 #0 SMP Sun Dec 6 07:31:03 2020 x86_64 GNU/Linux

my builder system:

Linux builderhost 5.8.0-63-generic #71-Ubuntu SMP Tue Jul 13 15:59:12 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux
root@builderlinux:~$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/builderlinux/.cache/go-build"
GOENV="/home/builderlinux/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/builderlinux/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/builderlinux/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build877627111=/tmp/go-build -gno-record-gcc-switches"

Whenever you build into native machine code, you must use OpenWrt's cross-compiler (be it as part of the SDK or the full buildroot) - you can't build on a general purpose linux distribution and call it a day. Among (many-) other things, OpenWrt uses musl instead of glibc as system c-library, which is the most common case of this particular error message (referring to /lib64/ld-linux-x86-64.so.2 not being found).

3 Likes

Have you tried building with CGO_ENABLED=0? This should create a binary that does not depend on libc (but will only work if your program doesn't use any C libraries).

2 Likes

thanks
i tworked when i compiled program on x86_64 Openwrt

no luck same error occurs when i compiled that program on my linux machine:

on builder machine

env cgo_enabled=0 goos=linux goarch=amd64 go build test.go

i installed golang_1.16.7-1_x86_64.ipk on Openwrt
and tried

env CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build

it works ! thanks @janh

Environment variable names are case-sensitive (at least on Unix-like systems). So if you use the last command line (with uppercase environment variables) on your build machine it should also work.

1 Like

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.