caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* calling OCaml from C on non-Mac platforms?
@ 2009-11-15 13:27 John Nowak
  2009-11-15 14:55 ` [Caml-list] " Eric Cooper
  0 siblings, 1 reply; 3+ messages in thread
From: John Nowak @ 2009-11-15 13:27 UTC (permalink / raw)
  To: caml-list

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


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [Caml-list] calling OCaml from C on non-Mac platforms?
  2009-11-15 13:27 calling OCaml from C on non-Mac platforms? John Nowak
@ 2009-11-15 14:55 ` Eric Cooper
  2009-11-15 15:22   ` John Nowak
  0 siblings, 1 reply; 3+ messages in thread
From: Eric Cooper @ 2009-11-15 14:55 UTC (permalink / raw)
  To: caml-list

On Sun, Nov 15, 2009 at 08:27:49AM -0500, John Nowak wrote:
> I'm trying to call OCaml from C.
> [...] 
>    ocamlopt -output-obj hello.cmx -o hello.o

This is the main problem.  You're clobbering the hello.o that OCaml
produced when compiling hello.ml into native code.

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

This worked fine for me:

  ocamlopt -output-obj -o temp.o hello.ml
  gcc -o hello main.c temp.o -L/usr/lib/ocaml -lasmrun -lm -ldl


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [Caml-list] calling OCaml from C on non-Mac platforms?
  2009-11-15 14:55 ` [Caml-list] " Eric Cooper
@ 2009-11-15 15:22   ` John Nowak
  0 siblings, 0 replies; 3+ messages in thread
From: John Nowak @ 2009-11-15 15:22 UTC (permalink / raw)
  To: caml-list


On Nov 15, 2009, at 9:55 AM, Eric Cooper wrote:

> On Sun, Nov 15, 2009 at 08:27:49AM -0500, John Nowak wrote:
>
>> I'm trying to call OCaml from C.
>> [...]
>>   ocamlopt -output-obj hello.cmx -o hello.o
>
> This is the main problem.  You're clobbering the hello.o that OCaml
> produced when compiling hello.ml into native code.

Works well now, thanks. Perhaps the documentation could be amended to  
make this more clear. Although, in retrospect, I should've realized  
what was happening.

- John


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2009-11-15 15:22 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-11-15 13:27 calling OCaml from C on non-Mac platforms? John Nowak
2009-11-15 14:55 ` [Caml-list] " Eric Cooper
2009-11-15 15:22   ` John Nowak

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).