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/C interface
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Nathan Mishra Linger <nathan.mishralinger@g...>
Subject: OCaml/C interface
Having just read Chapter 18 of the manual regarding "Interfacing C with
OCaml", I wrote a simple program to test my understanding.  However, when I
try to compile it I run into some undefined symbol problems (_tgetent,
_tgetnum, etc).  My machine is a PowerPC iBook G4 running OS X 10.4.10.

Does anyone know how to fix this?  After Googling around, I think this is a
Mac problem rather than an OCaml one, but I thought people on this list
might have come up against this same problem.  Also, any comments on my C
code that calls OCaml would be appreciated.


prompt$ cat

let rec from_to x y = if x > y then [] else x :: from_to (x+1) y
let sum_list xs = List.fold_left (+) 0 xs

let _ = Callback.register "from_to" from_to
let _ = Callback.register "sum_list" sum_list
let _ = Callback.register "gc" Gc.full_major

prompt$ cat test.c
/* C includes */
#include <stdio.h>

/* OCaml includes */
#include "caml/mlvalues.h"
#include "caml/memory.h"
#include "caml/callback.h"

value up_to(int n) {
  CAMLreturn(caml_callback2(*caml_named_value("from_to"), Val_int(1),

void gc(void) {
  caml_callback(*caml_named_value("gc"), Val_unit);

int sum(value xs) {
  CAMLreturn(Int_val(caml_callback(*caml_named_value("sum_list"), xs)));

int main() {
  char* argv[]  = {"yo"};
  xs = up_to(5);
  printf("sum of [1..5] = %i\n", sum(xs));

prompt$ cat Makefile
        ocamlc -c
        cc -I/usr/local/lib/ocaml -c test.c
        ocamlc -output-obj -o camlcode.o test.cmo
        cc -o myprog test.o camlcode.o -L/usr/local/lib/ocaml -lcamlrun

        rm *.cm? *.o

prompt$ make
ocamlc -c
cc -I/usr/local/lib/ocaml -c test.c
ocamlc -output-obj -o camlcode.o test.cmo
cc -o myprog test.o camlcode.o -L/usr/local/lib/ocaml -lcamlrun
/usr/bin/ld: Undefined symbols:
collect2: ld returned 1 exit status
make: *** [all] Error 1