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
[Caml-list] Interfacing C and Ocaml
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Jung Woon Ho <unosoft@h...>
Subject: [Caml-list] Interfacing C and Ocaml
Hi, Im trying to implement a simple program that interfaces C and Ocaml.
Here is what I do:

        ocamlc -custom -output-obj -o modcaml.o
        ocamlc -c modwrap.c
        cp /usr/local/lib/ocaml/libcamlrun.a mod.a
        ar r mod.a modcaml.o modwrap.o
	cc -o prog main.c mod.a -lcurses

and I get the error:
cannot find -lcurses
collect2: ld returned 1 exit status

I tried cc -o prog main.c mod.a -lcurses -ltermcap and cc -o prog main.c 
mod.a -ltermcap but i cant get the program to link.

Does anybody know how to solve this problem?

Thank you very much.
(* File -- some ``useful'' Caml functions *)

let rec fib n = if n < 2 then 1 else fib(n-1) + fib(n-2)

let format_result n = Printf.sprintf "Result is: %d\n" n

(* Export those two functions to C *)

let _ = Callback.register "fib" fib
let _ = Callback.register "format_result" format_result

/* File modwrap.c -- wrappers around the Caml functions */

#include <stdio.h>
#include <string.h>
#include <caml/mlvalues.h>
#include <caml/callback.h>

int fib(int n)
  static value * fib_closure = NULL;
  if (fib_closure == NULL) fib_closure = caml_named_value("fib");
  return Int_val(callback(*fib_closure, Val_int(n)));

char * format_result(int n)
  static value * format_result_closure = NULL;
  if (format_result_closure == NULL)
    format_result_closure = caml_named_value("format_result");
  return strdup(String_val(callback(*format_result_closure, Val_int(n))));
  /* We copy the C string returned by String_val to the C heap
     so that it remains valid after garbage collection. */

/* File main.c -- a sample client for the Caml functions */

#include <stdio.h>

int main(int argc, char ** argv)
  int result;

  /* Initialize Caml code */
  /* Do some computation */
  result = fib(10);
  printf("fib(10) = %s\n", format_result(result));
  return 0;

Help STOP SPAM with the new MSN 8 and get 2 months FREE*

To unsubscribe, mail Archives:
Bug reports: FAQ:
Beginner's list: