LoginSignup
9
9

More than 5 years have passed since last update.

クロスコンパイルに感動した

Last updated at Posted at 2014-01-08

インストール

インストール
$ brew install go
クロスコンパイル用準備。一度だけ実行すれば良い。
$ cd /usr/local/Cellar/go/*/libexec/src
$ ./all.bash
  (5分待つ)
windows_32bit向けコンパイラ生成
$ cd /usr/local/Cellar/go/*/libexec/src
$ GOOS=windows GOARCH=386 ./make.bash
  (1分待つ)
  (../bin/windows_386/が生成される。)

プログラム書く

なにか書く

cd ~/src/
cat > hi.go <<EOF
package main

func main() {
    println("こんにちは")
}
EOF

動作確認

1.コンパイルせずに実行
$ go run hi.go
2.コンパイルして実行
$ go build hi.go
$ ./hi.go

ビルド(windows32bit)

windows32bit
$ GOOS=windows GOARCH=386 go build hi.go

hi.exeが生成される。
windows機に転送して動作を確認。

実行ファイルがどの環境向けかはfileコマンドで調べられます。

環境設定一覧

  • 自分が使う環境一覧
GOOS=windows GOARCH=386
GOOS=windows GOARCH=amd64
GOOS=linux GOARCH=386
GOOS=linux GOARCH=amd64
GOOS=linux GOARCH=arm

raspberry pi用にもビルドできて感動した。

実行ファイルの圧縮

  • upxを使い圧縮しましょう。
sudo apt-get install -y upx
$ upx a.exe
                       Ultimate Packer for eXecutables
                          Copyright (C) 1996 - 2013
UPX 3.91        Markus Oberhumer, Laszlo Molnar & John Reiser   Sep 30th 2013

        File size         Ratio      Format      Name
   --------------------   ------   -----------   -----------
   7692800 ->   2243584   29.16%    win64/pe     a.exe

vimで*.goのsyntax hilight

~/.vimrc
set runtimepath+=/usr/local/Cellar/go//1.2/libexec/misc/vim
auto BufNewFile,BufRead *.go set ft=go
9
9
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
9
9