Version française
Home     About     Download     Resources     Contact us    
Browse thread
calling OCaml from C on non-Mac platforms?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: John Nowak <john@j...>
Subject: calling OCaml from C on non-Mac platforms?
I'm trying to call OCaml from C. Essentially, I have this for "main.c":

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

    void hello() {
       static value *closure = NULL;
       if(!closure) closure = caml_named_value("hello");
       caml_callback(*closure, Val_unit);
    }

    int main(int argc, char **argv) {
       caml_main(argv);
       hello();
       return 0;
    }

And the following "hello.ml":

    let hello () = print_endline "Hello, world!"
    let () = Callback.register "hello" hello

I can then compile as follows on OS X 10.5:

    ocamlopt -c hello.ml -o hello.cmx
    ocamlopt -output-obj hello.cmx -o hello.o
    gcc -c main.c -o main.o -I"`ocamlc -where`"
    gcc -o hello hello.o main.o -L"`ocamlc -where`" -ldl -lm -lasmrun

This works fine on OS X. However, it does not seem to work on Linux/ 
AMD64 or FreeBSD/AMD64. I've tried suggestions from all over the web  
and I've tried the approach given in the manual and nothing works. In  
all non-Mac cases, I get this on the second line:

    hello.o: file not recognized: File truncated
    Error during linking

One thing I can do is do this instead of the first two lines:

    ocamlopt -c -output-obj hello.ml

However, then I get the following:

    hello.o: In function `camlHello__hello_58':
    (.text+0x8): undefined reference to  
`camlPervasives__print_endline_298'
    /usr/lib/ocaml/3.10.2/libasmrun.a(startup.o): In function  
`caml_main':
    (.text+0x25f): undefined reference to `caml_data_segments'
    /usr/lib/ocaml/3.10.2/libasmrun.a(startup.o): In function  
`caml_main':
    (.text+0x273): undefined reference to `caml_code_segments'
    ...

Does anyone have a correct set of instructions for doing this on non- 
Mac platforms? Thanks.

- John