Browse thread
OCaml/C interface
- Nathan Mishra Linger
[
Home
]
[ Index:
by date
|
by threads
]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
[ 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.
Thanks,
Nathan
==================================
prompt$ cat test.ml
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) {
CAMLparam0();
CAMLreturn(caml_callback2(*caml_named_value("from_to"), Val_int(1),
Val_int(n)));
}
void gc(void) {
caml_callback(*caml_named_value("gc"), Val_unit);
return;
}
int sum(value xs) {
CAMLparam1(xs);
CAMLreturn(Int_val(caml_callback(*caml_named_value("sum_list"), xs)));
}
int main() {
CAMLlocal1(xs);
char* argv[] = {"yo"};
caml_main(argv);
xs = up_to(5);
gc();
printf("sum of [1..5] = %i\n", sum(xs));
}
prompt$ cat Makefile
all:
ocamlc -c test.ml
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
clean:
rm *.cm? *.o
prompt$ make
ocamlc -c test.ml
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:
_tgetent
_tgetnum
_tgetstr
_tputs
collect2: ld returned 1 exit status
make: *** [all] Error 1