Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
Ocaml, Objective-c, and XCode
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2010-02-11 (11:16)
From: John Whitington <john@c...>
Subject: Re: [Caml-list] Ocaml, Objective-c, and XCode
Hi Andrew,

On 11/02/2010 01:28, Andrew Webb wrote:
> Hi there,
> I have a few questions relating to using Ocaml with objective-c for anyone who programs on a Mac. I am aware that there aren't any real bindings between ocaml and objective-c/cocoa, so I am investigating rolling my own. So:
> 1) Does anyone have experience in interfacing ocaml and objc? Is it difficult?
> 2) Does anyone have a small example project that does mix these two languages? I have looked at unison, but it is too big for me to get my head around. I am thinking more on the scale of the ubiquitous Currency Converter...
> 3) Does anyone who is mixing these languages use Xcode to do so? If so, what steps do you need to go through to set that up?
> A big thanks to anyone who can answer (some of) these questions. I am trying to decide whether to use haskell or ocaml for my next cocoa application, as these are the two languages I am most comfortable. I have recently found an excellent description of how to do this using haskell ( and was wondering if there were such resources for ocaml.

I've just released a product written in this way:

I just have a normal makefile which builds a .a library from the ocaml 
code and its C wrapper as per the instructions in the ocaml manual.

The xcode project then uses the .a and the .h of the wrapper and a few 
extra compilation and linking flags. Doubtless with a little bit more 
effort, Xcode could be made to call the ocaml makefile.

Here's the OCaml makefile:

         ocamlc cpdf.mli;
         ocamlopt -c -I . unix.cmxa str.cmxa bigarray.cmxa cgutil.cmxa 
         ocamlc cpdflib.mli;
         ocamlopt -c -I . unix.cmxa str.cmxa bigarray.cmxa cgutil.cmxa 
camlpdf.cmxa cpdf.cmx;
         ocamlc cpdflibc.mli;
         ocamlopt -c -I . unix.cmxa str.cmxa bigarray.cmxa cgutil.cmxa 
camlpdf.cmxa cpdf.cmx cpdflib.cmx;
         ocamlc cpdflibwrapper.c;
         ocamlopt -output-obj -o cpdflibc.o unix.cmxa str.cmxa 
bigarray.cmxa cgutil.cmxa camlpdf.cmxa cpdf.cmx cpdflib.cmx cpdflibc.cmx;
         cp /usr/local/lib/ocaml/libasmrun.a cpdflib.a;
         ar r cpdflib.a cpdflibc.o cpdflibwrapper.o

test:   zlibstubs.o cpdflib.a
         cc -m32 -c cpdflibc-test.c -o cpdflibc-test.o; \
         cc -m32 -L'/usr/local/lib/ocaml' -lunix -lbigarray -lstr -lz -o 
test cpdflibc-test.o zlibstubs.o cpdflib.a

(The 'test' target shows you the kind of compiler and linker flags you'd 
need to put into XCode).

Basically, once you've got the usual ocaml/c interface done, the Xcode 
part is no problem.


John Whitington
Director, Coherent Graphics Ltd