2009-09-27
_xを使えば、unused variable警告を抑制できるよ
Syntax |
みなさん、コンパイルするときに"-w A -warn-error A"つけてますか? わりと適切な警告がでて便利ですよ。
例えば、こんなコード書くと警告してくれます。
# let f x = ();; Warning Z: unused variable x. val f : 'a -> unit = <fun>
うん、うん、xを使ってないことを教えてくれたんだね、ありがとう。
で、普段はなんの問題もないんだけど、コードを自動生成したときとかは、この警告がうざいこともある。
そんなときは、先頭に_をつければ、警告を抑制できるよ。
# let f _x = ();; val f : 'a -> unit = <fun>
ワイルドカード・パターン(_)とは違うから、ちゃんと変数を使うこともできる。
# let f _x = _x +1;; val f : int -> int = <fun>
2009-09-02
SnowLeopardでOCamlは動くよ。しかも64bitで
Mac |
生協にジュースを買いに行ったら、なぜかSnowLeopardを買ってしまいました。
とりあえず最新のMacPortsを入れてport install ocamlをしたら、あっさりocamlがインストールできました。
しかも64bitでビルドされていました。
$ ocaml
Objective Caml version 3.11.1
# Sys.word_size;;
- : int = 64
これで、31bitのintに苦しめられることもありません。やったね!
2009-08-02
Macでxml-lightを使おう
Mac |
xml-light便利ですよね。厳密なスキーマのチェックとかが要らなくて、とりあえずXMLを扱いたいときとかにぴったり。
でもMacPortsでxml-lightをインストールしても、ocamlfindから見えなくて結構不便です。
なんとかしましょう。
何が問題なの?
# xml-lightをインストールした! $ sudo port install xml-light .... # でもocamlfindからは見えない>< $ ocamlfind list | grep xml-light
なぜocamlfindから見えないかというと、xml-lightのMakefileがocamlfindに対応していないからです。
ここで解決方法は2つあります。
- 自分でxml-lightをビルドする
- ボクの作ったPortfileを使う
方法1:自分でxml-lightをビルドする
普通にビルドする。うっかりmake installしちゃわないように注意してください。
$ wget http://tech.motion-twin.com/zip/xml-light-2.2.zip $ unzip xml-light-2.2.zip $ cd xml-light $ make all $ make opt
適当にMETAファイルを作ってやる。ボクは以下の内容をMETAというファイルに保存して使ってます。
name="xml-light" version="2.2" description="minimal XML parser & printer" requires="" archive(byte) = "xml-light.cma" archive(native) = "xml-light.cmxa"
ocamlfindを使ってインストールする。
$ vi META # 上記の内容を保存する .... $ ocamlfind install xml-light META xml-light.a xml-light.cma xml-light.cmxa *.cmo *.cmi *.cmx *.cmx"
これでocamlfindから使えるようになりました。
$ ocamlfind list | grep xml-light xml-light (version: 2.2)
方法2: ボクの作ったPortfileを使う
上で書いた内容をやってくれるPortfileを作りました。
port installするだけでxml-lightをインストールできます。
$ wget http://howdyworld.org/patches/Portfile
$ sudo port install
$ ocamlfind list | grep xml-light xml-light (version: 2.2)
Portfile
念のためPortfileの中身をここにも貼っておきますね。META.zipは、上記のMETAファイルをzipで圧縮しただけのファイルです。
propella2009/08/08 08:32書いてある通りにしたら上手く行きました。ありがとうございます!
2009-07-05
imenu.elで、関数定義にジャンプ
Emacs |
imenu.elを使うと、バッファ内の関数定義一覧を出して、そこにジャンプすることができるよ。TAGSと違って、ファイルの更新が不要だから、お手軽だよ。
インストール方法
imenu.el の使い方と応用 — ありえるえりあ を参考に、imenu.elとanything.elを設定する。
tuareg-imenu.elからtuareg-imenu.elをダウンロードして、load-pathの通ってるディレクトリに置く。
.emacsに設定を追加する。
(autoload 'tuareg-imenu-set-imenu "tuareg-imenu" "Configuration of imenu for tuareg" t) (add-hook 'tuareg-mode-hook (lambda () (tuareg-imenu-set-imenu))
omakeをインストールしよう
MacPortsでomakeをインストールしようとすると、 omakeがインストールできない - ふなむしのはあとふるだいありぃ!って怒られてしまいます。
OCaml3.11でomakeをビルドすると、発生するらしいです。
そんなときは慌てず騒がず、/opt/local/src/exec/omake_exec.mlからsyncをコメントアウトしましょう。
(* external sync : unit -> unit = "caml_sync" *)
パッチが、もうでてるんで、そのうち直るでしょう。
2009-03-11
ocamloptでユニバーサルバイナリが作りたい
Mac |
何が問題なの?
ocamloptで実行ファイルを作っても、単一のアーキテクチャでしか動かない><。
$ ocamlopt hello.ml -o hello
$ file hello
hello-i386: Mach-O executable i386
"-ccopt"で-archを複数指定してやればいいと思いきや、それでもうまく動かない><。
$ ocamlopt -ccopt "-arch i386 -arch ppc" hello.ml ld warning: in /var/folders/ER/ERpTAvKHGAeST-apMbQus++++TI/-Tmp-/camlstartup73a88f.o, file is not of required architecture ld warning: in /opt/manual/ocaml-spot/lib/ocaml/std_exit.o, file is not of required architecture ld warning: in hello.o, file is not of required architecture ld warning: in /opt/manual/ocaml-spot/lib/ocaml/stdlib.a, file is not of required architecture ld warning: in /opt/manual/ocaml-spot/lib/ocaml/libasmrun.a, file is not of required architecture Undefined symbols for architecture ppc: "_main", referenced from: start in crt1.10.5.o ld: symbol(s) not found for architecture ppc collect2: ld returned 1 exit status lipo: can't open input file: /var/folders/ER/ERpTAvKHGAeST-apMbQus++++TI/-Tmp-//ccwCVSY5.out (No such file or directory) File "caml_startup", line 1, characters 0-1: Error: Error during linking
で、どうすればいいの?
- Re: [Caml-list] Re: Building a universal binary on OS X?を参考にして、PPC用ocamloptとi386用ocamloptを作ってやる
- PPC用とi386用のバイナリをコンパイルする
- lipoで結合する
手順
$ ./configure -cc "gcc -mmacosx-version-min=10.4 -arch ppc -isysroot /Developer/SDKs/MacOSX10.4u.sdk" -prefix /opt/ppc -host powerpc-apple-darwin8.11.0 $ make world $ make opt $ sudo make install
いったんcleanする。
$ make clean
$ ./configure -cc "gcc -mmacosx-version-min=10.4 -arch i386 -isysroot /Developer/SDKs/MacOSX10.4u.sdk" -prefix /opt/i386 $ make world $ make opt $ sudo make install
じゃあ、コンパイルしてみよう。
$ /opt/ppc/bin/ocamlopt -o hello-ppc hello.ml $ /opt/i386/bin/ocamlopt -o hello-i386 hello.ml $ lipo -create -output hello -arch ppc hello-ppc -arch i386 hello-i386 $ file hello hello: Mach-O universal binary with 2 architectures hello (for architecture ppc): Mach-O executable ppc hello (for architecture i386): Mach-O executable i386
やった! できた!!
ついでだから、ocamlopt-pccみたいなシンボリックリンクを作っておくと便利だよ!
謝辞
@banjunにいろいろ教えてもらいました。ありがとう!

