Cross compile for Raspberry Pi

I wanted to try cross compiling gotop for the Raspberry Pi 4 using my Windows 10 Laptop. It threw me an error (Yikes!):

logging_other.go:11:2: undefined: syscall.Dup2

The day was growing late and I needed to get some grilling done. So I decided instead (for now) to go get it, build, and install on my Pi 4 and play with a simpler task, cross compiling for the ARM processor.

Update on the earlier error above (12/19) – To get around the Linux syscall error when cross compiling on Windows 10, run your go build in a GIT Bash (or equivalent) shell.

# go get gotop locally on my Pi 4
go get github.com/cjbassi/gotop

go build

go install

# See it running below

Here’s gotop, showing load across 4 CPU Cores, memory and process utilization. If you type the ‘?’ it will show you the key bindings, and if you know vim most of them will be familiar to you.

Now that I have gotop up and running (I hope to use it to see how loaded it gets using picamara in a VNC session), i’ll show the important parts of cross compiling from Windows 10 to the Pi 4.

Here’s a simple hello world that we’ll cross compile, I call the file howdy.go:

package main

import "fmt"

func main() {
	fmt.Println("Howdy do")
}

The Pi 4 uses an ARM 7 processor, but you can check by running the uname command.

uname -a

Linux my-host 4.19.75-v7l+ #1270 SMP Tue Sep 24 18:51:41 BST 2019 armv7l GNU/Linux

Compile the sample howdy.go for Linux specifying the ARM 7 processor:

REM Compile for ARM 7 processor
c:> env GOOS=linux GOARCH=arm GOARM=7 go build

REM copy to Pi4
scp howdy pi@my-host:~/

Give it a run on your Pi 4, making sure the permissions are good to run it.

chmod 755 howdy

./howdy

I hope you found this post both practical and interesting!

Leave a comment

Your email address will not be published. Required fields are marked *