caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: "Török Edwin" <edwin+ml-ocaml@etorok.net>
To: caml-list@inria.fr
Subject: Re: [Caml-list] static C library of wrappers: compilation issues
Date: Thu, 26 Apr 2012 11:33:45 +0300	[thread overview]
Message-ID: <4F990869.5050105@etorok.net> (raw)
In-Reply-To: <4F99003B.9070001@inria.fr>

On 04/26/2012 10:58 AM, Xavier ALLAMIGEON wrote:
> Dear caml-list,
> 
> I'd like to build a static C library implementing an interface to an OCaml library, but I get some compilation errors. Here's an example of the problem.
> 
> 1) The ml_code.* files contain a hello_world function which I'd like to provide in C.
> 
> <ml_code.ml>
> let ml_hello_world () =
>   print_endline "Hello world!"
> 
> let _ =
>   Callback.register "ml_hello_world" ml_hello_world
> 
> <ml_code.mli>
> val ml_hello_world: unit -> unit
> 
> 2) On the C part, I created .c/.h files calling the ml function with camlback:
> <c_code.c>
> #include <caml/callback.h>
> #include <caml/memory.h>
> 
> void init(void) {
>   char* dummy = '\0';
>   caml_main(&dummy);
> }
> 
> void c_hello_world(void) {
>   CAMLparam0();
>   static value *closure_ml_hello_world = NULL;
>   if (closure_ml_hello_world == NULL) {
>     closure_ml_hello_world = caml_named_value("ml_hello_world");
>   }
>   caml_callback(*closure_ml_hello_world, Val_unit);
>   CAMLreturn0;
> }
> 
> <c_code.h>
> void init(void);
> void c_hello_world(void);
> 
> 3) Finally, I created a test file in C:
> <test.c>
> #include "c_code.h"
> 
> int main(int argc, char **argv) {
>   init();
>   c_hello_world();
> }
> 
> Here's the way I'm compiling everything. It builds a libhello_world.a static library from ml_code.obj.o and c_code.o.
> ocamlc -c ml_code.mli
> ocamlopt -c ml_code.ml
> ocamlopt -output-obj ml_code.cmx -o ml_code.obj.o
> gcc -c c_code.c -I"`ocamlc -where`"
> ar rcs libhello_world.a ml_code.obj.o c_code.o
> gcc -o test -L. -L"`ocamlc -where`" test.c -lhello_world -lasmrun -lm -ldl

You need -Wl,--whole-archive -Wl,--no-whole-archive around the link of your .a, like this:
gcc -o test -L. -L"`ocamlc -where`" test.c -Wl,--whole-archive -lhello_world -Wl,--no-whole-archive -lasmrun -lm -ldl

Otherwise the functions from your .a are dropped because nothing else before it needs it (-lasmrun would, but its after your .a).


Best regards,
--Edwin

  reply	other threads:[~2012-04-26  8:33 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-04-26  7:58 Xavier ALLAMIGEON
2012-04-26  8:33 ` Török Edwin [this message]
2012-04-26 10:08   ` Xavier ALLAMIGEON
2012-04-26 12:23     ` Alain Frisch
2012-04-26 14:30       ` Xavier ALLAMIGEON

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=4F990869.5050105@etorok.net \
    --to=edwin+ml-ocaml@etorok.net \
    --cc=caml-list@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).