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!

Mitch enjoys tinkering with tech across a wide range of disciplines. He loves learning new things and sharing his interests. His work interests run the gamut of: application integration, scalable secure clusters, embedded systems, and user interfaces. After hours you might find him dabbling in the hobby space with Raspberry Pi's, drones, photography, home wine making and other ferments.

Published by Mitch Dresdner

Mitch enjoys tinkering with tech across a wide range of disciplines. He loves learning new things and sharing his interests. His work interests run the gamut of: application integration, scalable secure clusters, embedded systems, and user interfaces. After hours you might find him dabbling in the hobby space with Raspberry Pi's, drones, photography, home wine making and other ferments.

Leave a comment

You can comment using your social media account

%d bloggers like this: