<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE message PUBLIC
  "-//MLarc//DTD MLarc output files//EN"
  "../../mlarc.dtd"[
  <!ATTLIST message
    listname CDATA #REQUIRED
    title CDATA #REQUIRED
  >
]>

  <?xml-stylesheet href="../../mlarc.xsl" type="text/xsl"?>


<message 
  url="2009/01/eabe0f1126a9d8c37e11c08054d99a36"
  from="Ben Jakb &lt;ben.jakb@g...&gt;"
  author="Ben Jakb"
  date="2009-01-10T01:22:05"
  subject="Re: [Caml-list] Shared libraries with ocamlopt callable from C (without main())?"
  prev="2009/01/3884db1d1064e8341b9e0ba01c22eb72"
  next="2009/01/df2de1846d5559d3faa62662b822bba6"
  prev-in-thread="2009/01/c2641b2a52606a6aff3aabe0204177b0"
  next-in-thread="2009/01/9cb3439eed351c14973ea021f31f2a83"
  prev-thread="2009/01/91c2e13999073da72a0efffe4acd71e6"
  next-thread="2009/01/2112b46e78c48d5a1e210bbd83ebb31a"
  root="../../"
  period="month"
  listname="caml-list"
  title="Archives of the Caml mailing list">

<thread subject="Shared libraries with ocamlopt callable from C (without main())?">
<msg 
  url="2009/01/5c289b2a10ec7c701b7b196284e19c78"
  from="Ben Aurel &lt;ben.aurel@g...&gt;"
  author="Ben Aurel"
  date="2009-01-08T22:46:53"
  subject="Shared libraries with ocamlopt callable from C (without main())?">
<msg 
  url="2009/01/7143e848af5c993fe0de325d727426b6"
  from="Matthieu Dubuget &lt;matthieu.dubuget@g...&gt;"
  author="Matthieu Dubuget"
  date="2009-01-09T08:21:47"
  subject="Re: [Caml-list] Shared libraries with ocamlopt callable from C (without main())?">
<msg 
  url="2009/01/c2641b2a52606a6aff3aabe0204177b0"
  from="RABIH.ELCHAAR@s..."
  author="RABIH.ELCHAAR@s..."
  date="2009-01-09T08:47:02"
  subject="RE: [Caml-list] Shared libraries with ocamlopt callable from C(without main())?">
</msg>
<msg 
  url="2009/01/eabe0f1126a9d8c37e11c08054d99a36"
  from="Ben Jakb &lt;ben.jakb@g...&gt;"
  author="Ben Jakb"
  date="2009-01-10T01:22:05"
  subject="Re: [Caml-list] Shared libraries with ocamlopt callable from C (without main())?">
<msg 
  url="2009/01/9cb3439eed351c14973ea021f31f2a83"
  from="Matthieu Dubuget &lt;matthieu.dubuget@g...&gt;"
  author="Matthieu Dubuget"
  date="2009-01-10T11:50:49"
  subject="Re: [Caml-list] Shared libraries with ocamlopt callable from C (without main())?">
<msg 
  url="2009/01/7d3913d17469c862ee3e5ff3fceb4c1b"
  from="Ben Jakb &lt;ben.jakb@g...&gt;"
  author="Ben Jakb"
  date="2009-01-10T15:28:28"
  subject="Re: [Caml-list] Shared libraries with ocamlopt callable from C (without main())?">
<msg 
  url="2009/01/d52308de08216f29d09caa52b2f05dcf"
  from="Matthieu Dubuget &lt;matthieu.dubuget@g...&gt;"
  author="Matthieu Dubuget"
  date="2009-01-10T15:36:44"
  subject="Re: [Caml-list] Shared libraries with ocamlopt callable from C (without main())?">
<msg 
  url="2009/01/7504deae4c49192ff0d96985d8da8c12"
  from="Ben Jakb &lt;ben.jakb@g...&gt;"
  author="Ben Jakb"
  date="2009-01-10T18:08:40"
  subject="Re: [Caml-list] Shared libraries with ocamlopt callable from C (without main())?">
</msg>
</msg>
</msg>
<msg 
  url="2009/01/9b0dd581f47b1eb4890a146553820492"
  from="Ben Jakb &lt;ben.jakb@g...&gt;"
  author="Ben Jakb"
  date="2009-01-10T18:18:05"
  subject="Re: Shared libraries with ocamlopt callable from C (without main())?">
</msg>
</msg>
<msg 
  url="2009/01/c69b845f9974f5a4f59496ff3c48f981"
  from="Xavier Leroy &lt;Xavier.Leroy@i...&gt;"
  author="Xavier Leroy"
  date="2009-01-11T16:28:02"
  subject="Re: [Caml-list] Shared libraries with ocamlopt callable from C	(without main())?">
</msg>
</msg>
</msg>
</msg>
</thread>

<contents>
&gt; Could you point out what problem you faced?

I try to create a static library called "libadd5wrapperlib.a" ( I know
kinda WTF ). So here is my simple example:

(Warning: You'll see a lot of bad code below, sorry for that, I work
hard to improve)

=====  $ cat ./add5.ml =====
    (* the code doesnt add anything yet, it just gives 5 back *)
    let add_five () =
      let i = ref 0 in
        i := 5;
        !i;;
    Callback.register "add five" add_five;;


=====  $ cat ./add5wrapperlib.c (Wraps the Ocaml code ) =====
    #include &lt;stdio.h&gt;
    #include &lt;caml/mlvalues.h&gt;
    #include &lt;caml/callback.h&gt;
    int add5wrapper(char **argv) {
        int add_5;
        caml_startup(argv);
        add_5=Int_val(callback(*caml_named_value("add five"),Val_unit));
        return add_5;
}

=====  $ cat ./include/libadd5wrapper.h (header file) =====
    extern int add5 (char **argv);

=====  $ cat ./main.c =====
    #include &lt;stdio.h&gt;
    #include "libadd5wrapper.h"
    int main (int argc,char **argv){
       printf("Gimme - %d \n", add5wrapper());
       return 0;
}

Now I try to BUILD the whole thing:
----------------------------------------------
# 1.) Build the OCaml code
ocamlopt -output-obj add5.ml -o add5-prog.o

# 2.) Create the static library
gcc -c add5wrapperlib.c
ar rs lib/libadd5wrapperlib.a add5wrapperlib.o

# 3.) Create the main object file
gcc -c main.c -Iinclude -I"`ocamlc -where`"

# 4.) Build the whole thing:
gcc --static -Iinclude -Llib -o mainprog.opt main.o add5wrapperlib.o
-L"`ocamlc -where`" -ladd5wrapperlib -lm -ldl -lasmrun

Number 4 is where it breaks. I get a ton of errors and this is where
it ends for me. Because # 4 is also the point where I definitely have
no idea what I'm doing.
Thanks for helping me.

Errors of #4:
/usr/local/godi/lib/ocaml/std-lib/libasmrun.a(startup.o): In function
`caml_main':
startup.c:(.text+0x209): undefined reference to `caml_data_segments'
startup.c:(.text+0x21d): undefined reference to `caml_code_segments'
/usr/local/godi/lib/ocaml/std-lib/libasmrun.a(floats.o): In function
`caml_ceil_float':
floats.c:(.text+0x225): undefined reference to `ceil'
/usr/local/godi/lib/ocaml/std-lib/libasmrun.a(floats.o): In function
`caml_atan2_float':
floats.c:(.text+0x24b): undefined reference to `atan2'
/usr/local/godi/lib/ocaml/std-lib/libasmrun.a(floats.o): In function
`caml_atan_float':
floats.c:(.text+0x268): undefined reference to `atan'
/usr/local/godi/lib/ocaml/std-lib/libasmrun.a(floats.o): In function
`caml_acos_float':
floats.c:(.text+0x285): undefined reference to `acos'
/usr/local/godi/lib/ocaml/std-lib/libasmrun.a(floats.o): In function
`caml_asin_float':
floats.c:(.text+0x2a2): undefined reference to `asin'
/usr/local/godi/lib/ocaml/std-lib/libasmrun.a(floats.o): In function
`caml_tanh_float':
floats.c:(.text+0x2bf): undefined reference to `tanh'
/usr/local/godi/lib/ocaml/std-lib/libasmrun.a(floats.o): In function
`caml_tan_float':
floats.c:(.text+0x2dc): undefined reference to `tan'
/usr/local/godi/lib/ocaml/std-lib/libasmrun.a(floats.o): In function
`caml_cosh_float':
floats.c:(.text+0x2f9): undefined reference to `cosh'
/usr/local/godi/lib/ocaml/std-lib/libasmrun.a(floats.o): In function
`caml_cos_float':
floats.c:(.text+0x316): undefined reference to `cos'
/usr/local/godi/lib/ocaml/std-lib/libasmrun.a(floats.o): In function
`caml_sinh_float':
floats.c:(.text+0x333): undefined reference to `sinh'
/usr/local/godi/lib/ocaml/std-lib/libasmrun.a(floats.o): In function
`caml_sin_float':
floats.c:(.text+0x350): undefined reference to `sin'
/usr/local/godi/lib/ocaml/std-lib/libasmrun.a(floats.o): In function
`caml_power_float':
floats.c:(.text+0x376): undefined reference to `pow'
/usr/local/godi/lib/ocaml/std-lib/libasmrun.a(floats.o): In function
`caml_sqrt_float':
floats.c:(.text+0x3a6): undefined reference to `sqrt'
/usr/local/godi/lib/ocaml/std-lib/libasmrun.a(floats.o): In function
`caml_log10_float':
floats.c:(.text+0x487): undefined reference to `log10'
/usr/local/godi/lib/ocaml/std-lib/libasmrun.a(floats.o): In function
`caml_log_float':
floats.c:(.text+0x4a4): undefined reference to `log'
/usr/local/godi/lib/ocaml/std-lib/libasmrun.a(floats.o): In function
`caml_fmod_float':
floats.c:(.text+0x599): undefined reference to `fmod'
/usr/local/godi/lib/ocaml/std-lib/libasmrun.a(floats.o): In function
`caml_floor_float':
floats.c:(.text+0x5b6): undefined reference to `floor'
/usr/local/godi/lib/ocaml/std-lib/libasmrun.a(floats.o): In function
`caml_exp_float':
floats.c:(.text+0x5d3): undefined reference to `exp'
/usr/local/godi/lib/ocaml/std-lib/libasmrun.a(unix.o): In function
`caml_dlerror':
unix.c:(.text+0x1db): undefined reference to `dlerror'
/usr/local/godi/lib/ocaml/std-lib/libasmrun.a(unix.o): In function `caml_dlsym':
unix.c:(.text+0x1f5): undefined reference to `dlsym'
/usr/local/godi/lib/ocaml/std-lib/libasmrun.a(unix.o): In function
`caml_dlclose':
unix.c:(.text+0x208): undefined reference to `dlclose'
/usr/local/godi/lib/ocaml/std-lib/libasmrun.a(unix.o): In function
`caml_dlopen':
unix.c:(.text+0x223): undefined reference to `dlopen'
/usr/local/godi/lib/ocaml/std-lib/libasmrun.a(i386.o): In function
`caml_start_program':
(.text+0x129): undefined reference to `caml_program'
/usr/local/godi/lib/ocaml/std-lib/libasmrun.a(i386.o): In function
`caml_callback2_exn':
(.text+0x221): undefined reference to `caml_apply2'
/usr/local/godi/lib/ocaml/std-lib/libasmrun.a(i386.o): In function
`caml_callback3_exn':
(.text+0x241): undefined reference to `caml_apply3'
/usr/local/godi/lib/ocaml/std-lib/libasmrun.a(fail.o): In function
`caml_array_bound_error':
fail.c:(.text+0x75): undefined reference to `caml_exn_Invalid_argument'
/usr/local/godi/lib/ocaml/std-lib/libasmrun.a(fail.o): In function
`caml_raise_stack_overflow':
fail.c:(.text+0x98): undefined reference to `caml_bucket_Stack_overflow'
/usr/local/godi/lib/ocaml/std-lib/libasmrun.a(fail.o): In function
`caml_raise_out_of_memory':
fail.c:(.text+0xaa): undefined reference to `caml_bucket_Out_of_memory'
/usr/local/godi/lib/ocaml/std-lib/libasmrun.a(fail.o): In function
`caml_raise_sys_error':
fail.c:(.text+0x144): undefined reference to `caml_exn_Sys_error'
/usr/local/godi/lib/ocaml/std-lib/libasmrun.a(fail.o): In function
`caml_invalid_argument':
fail.c:(.text+0x17d): undefined reference to `caml_exn_Invalid_argument'
/usr/local/godi/lib/ocaml/std-lib/libasmrun.a(fail.o): In function
`caml_failwith':
fail.c:(.text+0x196): undefined reference to `caml_exn_Failure'
/usr/local/godi/lib/ocaml/std-lib/libasmrun.a(fail.o): In function
`caml_raise_sys_blocked_io':
fail.c:(.text+0x21a): undefined reference to `caml_exn_Sys_blocked_io'
/usr/local/godi/lib/ocaml/std-lib/libasmrun.a(fail.o): In function
`caml_raise_not_found':
fail.c:(.text+0x22c): undefined reference to `caml_exn_Not_found'
/usr/local/godi/lib/ocaml/std-lib/libasmrun.a(fail.o): In function
`caml_raise_zero_divide':
fail.c:(.text+0x23e): undefined reference to `caml_exn_Division_by_zero'
/usr/local/godi/lib/ocaml/std-lib/libasmrun.a(fail.o): In function
`caml_raise_end_of_file':
fail.c:(.text+0x250): undefined reference to `caml_exn_End_of_file'
/usr/local/godi/lib/ocaml/std-lib/libasmrun.a(roots.o): In function
`caml_init_frame_descriptors':
roots.c:(.text+0x118): undefined reference to `caml_frametable'
roots.c:(.text+0x136): undefined reference to `caml_frametable'
roots.c:(.text+0x18f): undefined reference to `caml_frametable'
roots.c:(.text+0x207): undefined reference to `caml_frametable'
/usr/local/godi/lib/ocaml/std-lib/libasmrun.a(roots.o): In function
`caml_do_roots':
roots.c:(.text+0x25c): undefined reference to `caml_globals'
roots.c:(.text+0x293): undefined reference to `caml_globals'
/usr/local/godi/lib/ocaml/std-lib/libasmrun.a(roots.o): In function
`caml_oldify_local_roots':
roots.c:(.text+0x364): undefined reference to `caml_globals'
roots.c:(.text+0x36b): undefined reference to `caml_globals'
collect2: ld returned 1 exit status















2009/1/9 Matthieu Dubuget &lt;matthieu.dubuget@gmail.com&gt;:
&gt; Ben Aurel a écrit :
&gt;&gt; I'm thinking about the possibility to use OCaml as an extension
&gt;&gt; language. If I wanted to call a OCaml function from langX I had to use
&gt;&gt; a C wrapper.
&gt;&gt;
&gt;&gt;     langX -&gt; ( C API of langX | C Wrapper | C API of OCaml ) -&gt; OCaml
&gt;&gt;
&gt;
&gt; Yes
&gt;
&gt;&gt; But could somebody tell me if ( and maybe how ) this can be done. I made some attempts, but
&gt;&gt; there is always a main() function involved. But this C wrapper would be a shared library.
&gt;&gt;
&gt;
&gt; Exactly. You can deliver your OCaml code as shared library, and then
&gt; call it from
&gt; any language that can call a shared library.
&gt;
&gt; Could you point out what problem you faced?
&gt;
&gt; Salutations
&gt;
&gt; Matt
&gt;
&gt;
&gt; _______________________________________________
&gt; Caml-list mailing list. Subscription management:
&gt; http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
&gt; Archives: http://caml.inria.fr
&gt; Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
&gt; Bug reports: http://caml.inria.fr/bin/caml-bugs
&gt;

</contents>

</message>

