Cross compile go apps on Windows 10
Join the DZone community and get the full member experience.
Join For Free
I wanted to try cross-compiling gotop for the Raspberry Pi 4 using my Windows 10 Laptop. It threw me an error (Yikes!):
xxxxxxxxxx
logging_other.go:11:2: undefined: syscall.Dup2
It was getting late, and I needed to get some grilling done. So I decided instead (for now) to go get it, build it, and install it on my Pi 4 and play with a simpler task: cross-compiling for the ARM processor.
xxxxxxxxxx
# 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 "?" operator 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.
You may also like: Cross-Compilation With Buildroot.
Here's a simple hello world that we'll cross-compile. I'll call the file howdy.go:
xxxxxxxxxx
package main
import "fmt"
func main() {
fmt.Println("Howdy do")
}
The Pi 4 uses an ARM 7 processor, but you can check this by running the uname command.
xxxxxxxxxx
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:
xxxxxxxxxx
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:~/
Run it on your Pi 4, making sure the permissions are good to run it.
xxxxxxxxxx
chmod 755 howdy
./howdy
I hope you found this post both practical and interesting!
Further Reading
Opinions expressed by DZone contributors are their own.
Comments