Life So Short, the Craft So Long to Learn

OCaml 4.01 for iOS 8 Simulator

December 19, 2014

OCamlXARM compiles for an iOS device, but OCamlXSim compiles for an iOS Simulator. The same ocamloptrev script that compiles OCaml for iOS 8 can also get OCamlXSim to compile OCaml for the iOS 8 Simulator. The only thing that changes is the location of the compiler.

If you want to try out OCaml on the iOS 8 Simulator, here is an update to the script that compiles for either an iOS device or an iOS Simulator (ocamloptrev):

# ocamloptrev     ocamlopt for specified iOS revision
USAGE='ocamloptrev  -rev M.N  [ -sim ]  other-ocamlopt-options ...'


declare -a ARGS
while [ $# -gt 0 ] ; do
    case $1 in
        if [ $# -gt 1 ]; then
            shift 2
            echo "$USAGE" >&2
            exit 1
    *)  ARGS[${#ARGS[*]}]="$1"
        shift 1
if [ "$REV" = "" ]; then
    echo "$USAGE" >&2
    exit 1


case $SIM in
y)  PLT=$HIDEOUT/Platforms/iPhoneSimulator.platform 
n)  PLT=$HIDEOUT/Platforms/iPhoneOS.platform 

$OCAMLC -ccopt -isysroot -ccopt "$PLT$SDK" "${ARGS[@]}"

To compile for the iOS Simulator, specify -sim along with -rev M.N.

Let’s make a tiny OCaml program for testing:

$ Q="Do you know what it's like on the outside?\\n"
$ echo "Printf.printf \"$Q\"" >

Here’s what happens if you compile with the current OCamlXSim on a system with the iOS 8.1 SDK:

$ /usr/local/ocamlxsim/bin/ocamlopt -o ny1941
clang: warning: no such sysroot directory: '/Applications/'
ld: library not found for -lSystem
clang: error: linker command failed with exit code 1 (use -v to see invocation)
File "caml_startup", line 1:
Error: Error during linking

As you can see, it’s trying and failing to use the default iOS Simulator 7.1 SDK. Here’s how to use ocamloptrev (the above script):

$ ocamloptrev -sim -rev 8.1 -o ny1941
$ ls -l ny1941
-rwxr-xr-x  1 jeffsco  staff  303364 Dec 19 23:02 ny1941
$ file ny1941
ny1941: Mach-O executable i386

You can actually run an iOS simulator app from the OS X command line, though there are many things that don’t work properly.

$ ny1941
Do you know what it's like on the outside?

See iOS Simulator Vs. OS X for a description of some differences between the OS X and the iOS Simulator environments.

If you don’t specify -sim, the script compiles for an iOS device as before:

$ ocamloptrev -rev 8.1 -o ny1941 -cclib -Wl,-no_pie
$ file ny1941
ny1941: Mach-O executable arm

When not working in the subbasement of my alma mater, I’m working in my cluttered underground workroom on several OCaml-on-iOS projects. Along with holiday joys and the delights of coding in node.js during the day, I’ll keep working through them as fast as I can.

I hope this script will be useful for folks who want to try OCaml on the iOS Simulator while I’m updating my humble patches to the latest versions of everything and keeping all the irons in the fire.

If you have any trouble (or success) with the script, or have any other comments, leave them below or email me at

Posted by: Jeffrey


blog comments powered by Disqus