LoginSignup
5
5

More than 5 years have passed since last update.

MacOS X でGo言語のクロスコンパイルを試したらハマった

Posted at

とりあえず試してみる

Go言語は簡単にクロスコンパイルができるという噂を聞いたので、早速試してみる。

$ GOOS=linux GOARCH=amd64 go build helloworld.go
$ ll
total 3800
-rwxr-xr-x  1 ttsuzo  staff  1941376  7 29 00:55 helloworld
-rw-r--r--  1 ttsuzo  staff      134  7 28 00:29 helloworld.go

できた!

では次にwindowsで・・・
が、GOOS=windowsを試しても.exeファイルができない。
ので、以下を参考にいろいろ試してみる。

見ていると、どうやらコンパイルで設定する実行環境を変えるにはいちいち.makeファイルを叩いて設定を変更しなければならないらしい。
が、変更しようとすると、

$ GOOS=linux GOARCH=amd64 ./make.bash
# Building C bootstrap tool.
cmd/dist
ld: can't open output file for writing 'cmd/dist/dist.ld_bEi247', errno=13 for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

こんなエラーが出ていたので、さらにググった結果ここを見て解決。

bash-3.2# GOOS=windows GOARCH=amd64 ./make.bash

もう一度実行

bash-3.2# GOOS=windows GOARCH=amd64 go build helloworld.go
bash-3.2# ll
total 4040
-rwxr-xr-x  1 root            staff  2061824  7 29 00:57 helloworld.exe
-rw-r--r--  1 tsuyoshi-chujo  staff      134  7 28 00:29 helloworld.go

おお、なんとなくできた。
でも確かに他のブログにも書かれている通り面倒臭い。。。

Goxを導入してみる

面倒臭いので調べていたら、どうやらGoxというのがあるらしい。
* mitchellh/gox - Github
* これからGoを始める人のためのTips集

という訳で、READMEの通りにやってみる。

$ go get github.com/mitchellh/gox
package github.com/mitchellh/gox: cannot download, $GOPATH not set. For more details see: go help gopath

いきなりエラー。
$GOPATHがセットされいないらしいので、セットする。

$ export GOPATH=$HOME/workspace/go/third-party:$HOME/workspace/go/my-project
$ export PATH=$HOME/workspace/go/third-party/bin:$HOME/workspace/go/my-project/bin:$PATH

改めて実行

$ go get github.com/mitchellh/gox

特にエラーが出ないので多分成功。

あとは記載の通り、gox -build-toolchain
のはずが、またエラー

Stderr: ld: can't open output file for writing 'cmd/dist/dist.ld_UOH2oy', errno=13 for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

見た感じ先ほど出ていたのと同じなので、同じように解決。

で、改めてビルド

$ gox
Number of parallel builds: 4

-->      darwin/386: _/Users/ttsuzo/workspace/go/my-project
-->    darwin/amd64: _/Users/ttsuzo/workspace/go/my-project
-->       linux/386: _/Users/ttsuzo/workspace/go/my-project
-->     linux/amd64: _/Users/ttsuzo/workspace/go/my-project
-->       linux/arm: _/Users/ttsuzo/workspace/go/my-project
-->     freebsd/386: _/Users/ttsuzo/workspace/go/my-project
-->   freebsd/amd64: _/Users/ttsuzo/workspace/go/my-project
-->     openbsd/386: _/Users/ttsuzo/workspace/go/my-project
-->   openbsd/amd64: _/Users/ttsuzo/workspace/go/my-project
-->     windows/386: _/Users/ttsuzo/workspace/go/my-project
-->   windows/amd64: _/Users/ttsuzo/workspace/go/my-project
-->     freebsd/arm: _/Users/ttsuzo/workspace/go/my-project
-->      netbsd/386: _/Users/ttsuzo/workspace/go/my-project
-->    netbsd/amd64: _/Users/ttsuzo/workspace/go/my-project
-->      netbsd/arm: _/Users/ttsuzo/workspace/go/my-project
bash-3.2# ll
total 50688
-rw-r--r--  1 ttsuzo  staff      134  7 28 00:29 helloworld.go
-rwxr-xr-x  1 root            staff  1550264  7 29 01:16 my-project_darwin_386
-rwxr-xr-x  1 root            staff  1923696  7 29 01:16 my-project_darwin_amd64
-rwxr-xr-x  1 root            staff  1548288  7 29 01:16 my-project_freebsd_386
-rwxr-xr-x  1 root            staff  1936760  7 29 01:16 my-project_freebsd_amd64
-rwxr-xr-x  1 root            staff  1586008  7 29 01:16 my-project_freebsd_arm
-rwxr-xr-x  1 root            staff  1564864  7 29 01:16 my-project_linux_386
-rwxr-xr-x  1 root            staff  1941392  7 29 01:16 my-project_linux_amd64
-rwxr-xr-x  1 root            staff  1585624  7 29 01:16 my-project_linux_arm
-rwxr-xr-x  1 root            staff  1551864  7 29 01:16 my-project_netbsd_386
-rwxr-xr-x  1 root            staff  1936856  7 29 01:16 my-project_netbsd_amd64
-rwxr-xr-x  1 root            staff  1585520  7 29 01:16 my-project_netbsd_arm
-rwxr-xr-x  1 root            staff  1547144  7 29 01:16 my-project_openbsd_386
-rwxr-xr-x  1 root            staff  1927432  7 29 01:16 my-project_openbsd_amd64
-rwxr-xr-x  1 root            staff  1671680  7 29 01:16 my-project_windows_386.exe
-rwxr-xr-x  1 root            staff  2061824  7 29 01:16 my-project_windows_amd64.exe

できた!
これなら環境ごとにいろいろコマンド実行しなくて済む。ついでにコマンド実行しなくて済むようにするためのスクリプトなんかも用意しなくて済む。

Gox自体も使いやすくて嬉しい。

おまけ

GoってHomebrewでインストールできたのか。。。公式サイトにもわかりやすいところに書いておいてほしかった。。。

成果物の動作確認はまた今度で。

おわり

5
5
1

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
5
5