{{announcement.body}}
{{announcement.title}}

Cross compile go apps on Windows 10

DZone 's Guide to

Cross compile go apps on Windows 10

In this article, we provide a brief tutorial on cross-compiling Go applications for a Raspberry Pi 4 on Windows 10.

· Web Dev Zone ·
Free Resource


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

Shell
xxxxxxxxxx
1
 
1
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 itbuild it, and install it on my Pi 4 and play with a simpler task: cross-compiling for the ARM processor.

Shell
xxxxxxxxxx
1
 
1
# go get gotop locally on my Pi 4
2
go get github.com/cjbassi/gotop
3
 
          
4
go build
5
 
          
6
go install
7
 
          
8
# 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:

Go
xxxxxxxxxx
1
 
1
package main
2
 
          
3
import "fmt"
4
 
          
5
func main() {
6
    fmt.Println("Howdy do")
7
}


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

Shell
xxxxxxxxxx
1
 
1
uname -a
2
 
          
3
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:

Shell
xxxxxxxxxx
1
 
1
REM Compile for ARM 7 processor
2
c:> env GOOS=linux GOARCH=arm GOARM=7 go build
3
 
          
4
REM copy to Pi4
5
scp howdy pi@my-host:~/


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

Shell
xxxxxxxxxx
1
 
1
chmod 755 howdy
2
 
          
3
./howdy

I hope you found this post both practical and interesting!


Further Reading

Topics:
raspberry pi ,go ,web dev ,cross compile ,arm ,tutorial

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}