caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: John Nowak <john@johnnowak.com>
To: caml-list@yquem.inria.fr
Subject: calling OCaml from C on non-Mac platforms?
Date: Sun, 15 Nov 2009 08:27:49 -0500	[thread overview]
Message-ID: <EB0C6A62-9DD3-48E6-BAE8-AB68A78699A5@johnnowak.com> (raw)

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


             reply	other threads:[~2009-11-15 13:27 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-11-15 13:27 John Nowak [this message]
2009-11-15 14:55 ` [Caml-list] " Eric Cooper
2009-11-15 15:22   ` John Nowak

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=EB0C6A62-9DD3-48E6-BAE8-AB68A78699A5@johnnowak.com \
    --to=john@johnnowak.com \
    --cc=caml-list@yquem.inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).