LoginSignup
16
15

More than 5 years have passed since last update.

gomobileでiOS用のライブラリをビルドするまで

Posted at
$ cd
$ brew uninstall go
$ bash < <(curl -s -S -L https://raw.githubusercontent.com/moovweb/gvm/master/binscripts/gvm-installer)
$ vi .zprofile.local
.zprofile.local
# gvm
[[ -s "$HOME/.gvm/scripts/gvm" ]] && source "$HOME/.gvm/scripts/gvm"
  • まず、gomobileはGo 1.5以降が必要なので、HomebrewをやめてgvmでGoのバージョン管理をする。
$ source .zprofile
$ gvm install go1.4.2
$ gvm use go1.4.2 --default
$ GOROOT_BOOTSTRAP=$GOROOT gvm install go1.5beta2
$ gvm use go1.5beta2 --default
$ go get golang.org/x/mobile/cmd/gomobile
$ gomobile init -v
  • Go 1.5のインストールにはGo 1.4以降が必要なので先に1.4.2をインストールする。
  • その後GOROOT_BOOTSTRAPで1.4.2のGOROOTを指定して1.5beta2をインストールする。
  • gomobile initでiOSやAndroid向けにツールチェーンをインストールしたり標準ライブラリをビルドしたりしているようだ。
$ cd $GOROOT/src
$ mkdir gomobile_playground && cd $_
$ mkdir hello
$ vi hello/hello.go
gomobile_playground/hello/hello.go
package hello

func World() string {
    return "Hello, world!"
}
  • helloというパッケージをとりあえず書いてみる。
$ cd hello
$ gomobile bind -target ios
$ ls
hello.framework hello.go
  • gomobile bind -target iosでパッケージをiOS用のフレームワークにビルドする。
hello.framework/Versions/Current/Headers/Hello.h
// Objective-C API for talking to github.com/naoty/gomobile_playground/hello Go package.
//   gobind -lang=objc github.com/naoty/gomobile_playground/hello
//
// File is generated by gobind. Do not edit.

#ifndef __GoHello_H__
#define __GoHello_H__

#include <Foundation/Foundation.h>

FOUNDATION_EXPORT NSString* GoHelloWorld();

#endif
  • ヘッダーファイルを見てみると、確かに文字列を返す関数が定義されているようだ。
16
15
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
16
15