LoginSignup
0
1

More than 1 year has passed since last update.

Goのクロスコンパイル(MacでビルドしてRaspberry Pi Zeroで実行)

Posted at

Raspberry PiでGo(gin)のプログラムをビルドしたら割と時間がかかったので、手元のMacでRaspberry Pi用にビルドすることにした時のメモ

Goでビルド時のターゲットに指定できるプラットフォームを確認 

以下を実行するとビルドのターゲットに指定できるプラットフォームのリストが出力される。
Raspberry Pi ZeroでLinux系のOSを使用している場合はlinux/armが該当するのでクロスコンパイルが出来そう。

% go tool dist list
aix/ppc64
android/386
android/amd64
android/arm
android/arm64
darwin/amd64
darwin/arm64
dragonfly/amd64
freebsd/386
freebsd/amd64
freebsd/arm
freebsd/arm64
illumos/amd64
ios/amd64
ios/arm64
js/wasm
linux/386
linux/amd64
linux/arm
linux/arm64
linux/loong64
linux/mips
linux/mips64
linux/mips64le
linux/mipsle
linux/ppc64
linux/ppc64le
linux/riscv64
linux/s390x
netbsd/386
netbsd/amd64
netbsd/arm
netbsd/arm64
openbsd/386
openbsd/amd64
openbsd/arm
openbsd/arm64
openbsd/mips64
plan9/386
plan9/amd64
plan9/arm
solaris/amd64
windows/386
windows/amd64
windows/arm
windows/arm64

実際にRaspberry Pi用にビルドしてみる

以下のような感じでビルド時のプラットフォームを指定する事ができる

% GOOS='OSを記述' GOARCH='CPUなどのアーキテクチャーを記述' go build -o '出力する実行ファイル名を指定'

Raspberry Pi向けにビルドする際の例

% GOOS=linux GOARCH=arm go build -o raspberry_bin

まとめ

マシンリソースが貧弱なRaspberry Pi(13-14秒)では時間がかかっていたビルドがMac(0.2秒)では一瞬で終わるようになったので満足!と思ったが実行ファイルの転送などの時間を考えると微妙...
もっとビルドに時間が掛かる事があれば活用できそう

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1