|
List Info
Thread: "ocaml_beginners"::[] Making a general library from OCaml-code
|
|
| "ocaml_beginners"::[] Making a
general library from OCaml-code |
  Germany |
2008-06-03 05:45:21 |
|
Hello,
I want to write a library that can be accessed
like any other system library on a unix/linux system,
but I don't want to code it in C. I want to write it
in Ocaml, but want to use it as a system-library
that can be accessed by C-programs.
How to create this?
Do I have to use C-interfacing tricks to get such a thing
done?
TIA,
Oliver
__._,_.___
.
__,_._,___
|
| Re: "ocaml_beginners"::[]
Making a general library from OCaml-code |
  United Kingdom |
2008-06-03 06:51:05 |
|
On Tue, Jun 03, 2008 at 12:45:21PM +0200, Oliver Bandel wrote:
> I want to write a library that can be accessed
> like any other system library on a unix/linux system,
> but I don't want to code it in C. I want to write it
> in Ocaml, but want to use it as a system-library
> that can be accessed by C-programs.
>
> How to create this?
It's a bit tricky. I've never really pinned it down, but here are
some general comments:
- Look at the source to mod_caml. It builds a .so containing the
OCaml runtime. To do this I needed to patch the runtime (patch is
going into 3.11 apparently).
http://caml.inria.fr/mantis/view.php?id=3866
- To generate a shared library, you will need to generate PIC code.
In recent versions it's the default, but you may need to enable it
(ocamlopt -fPIC).
- You need to initialize the OCaml runtime.
- Beware of multiple C threads calling into the OCaml code.
BTW if you do work it out, *please* post a mini-project or the steps
you used on a web page somewhere.
Rich.
--
Richard Jones
Red Hat
__._,_.___
.
__,_._,___
|
| Re: "ocaml_beginners"::[]
Making a general library from OCaml-code |
  Germany |
2008-06-03 07:06:55 |
|
Helo Richard,
Zitat von Richard Jones < rich%40annexia.org">rich annexia.org>:
> On Tue, Jun 03, 2008 at 12:45:21PM +0200, Oliver Bandel wrote:
> > I want to write a library that can be accessed
> > like any other system library on a unix/linux system,
> > but I don't want to code it in C. I want to write it
> > in Ocaml, but want to use it as a system-library
> > that can be accessed by C-programs.
> >
> > How to create this?
>
> It's a bit tricky. I've never really pinned it down, but here are
> some general comments:
>
> - Look at the source to mod_caml. It builds a .so containing the
> OCaml runtime. To do this I needed to patch the runtime (patch is
> going into 3.11 apparently).
> http://caml.inria.fr/mantis/view.php?id=3866
Well, should I better wait until release of 3.11 for such kinds of
projects?!
>
> - To generate a shared library, you will need to generate PIC code.
> In recent versions it's the default, but you may need to enable it
> (ocamlopt -fPIC).
>
> - You need to initialize the OCaml runtime.
[...]
So I have C-code, and from there calling my OCaml-code,
right? So there is no direct way for it, it seems.
>
> - Beware of multiple C threads calling into the OCaml code.
>
> BTW if you do work it out, *please* post a mini-project or the steps
> you used on a web page somewhere.
[...]
Well, the stuff is not really huge, so I also can write it entirely
in C. That seems to me to be faster in the over-all process.
But maybe I will try it with OCaml later on.
Because it's a small lib, this also might be good for
exploring such kind of OCaml-based library....well,
we will see how I will decide in the future 
If I possibly make it, I will make some web-description available
then.
But first I want to have the stuff available
for myself (I think in some hours I have it ready in C).
Ciao,
Oliver
__._,_.___
.
__,_._,___
|
| Re: "ocaml_beginners"::[]
Making a general library from OCaml-code |
  United States |
2008-06-03 07:18:01 |
|
Oliver Bandel a écrit :
>
> Hello,
>
> I want to write a library that can be accessed
> like any other system library on a unix/linux system,
> but I don't want to code it in C. I want to write it
> in Ocaml, but want to use it as a system-library
> that can be accessed by C-programs.
>
> How to create this?
>
> Do I have to use C-interfacing tricks to get such a thing
> done?
>
> TIA,
> Oliver
The idea is to generate one C-object file from your Caml code.
The -output-obj command line option allows this.
You will alse need to register the entry points in your Caml code
(Callback module) in
order to be able to call it from C side.
Yes, a C part is necessary, in order to:
- initialize the Caml runtime system
- expose C API and call the relevant Caml code, eventually converting
data between both worlds
ODLL, written by Nicolas Canasse is not widely used, but automates this
for windows world. It may
be a good idea to have it evolve (or simply update it to current version
of OCaml if necessary). Maybe
after the natdynlink branch of ocaml tools will be published (I think
this will simplify all the process).
Hoping that this helps
Salutations
Matt
__._,_.___
.
__,_._,___
|
| Re: "ocaml_beginners"::[]
Making a general library from OCaml-code |
  United States |
2008-06-03 07:23:25 |
|
Oliver Bandel a écrit :
>
> Helo Richard,
>
> Zitat von Richard Jones < rich%40annexia.org">rich annexia.org>:
>
> > On Tue, Jun 03, 2008 at 12:45:21PM +0200, Oliver Bandel wrote:
> > > I want to write a library that can be accessed
> > > like any other system library on a unix/linux system,
> > > but I don't want to code it in C. I want to write it
> > > in Ocaml, but want to use it as a system-library
> > > that can be accessed by C-programs.
> > >
> > > How to create this?
> >
> > It's a bit tricky. I've never really pinned it down, but here are
> > some general comments:
> >
> > - Look at the source to mod_caml. It builds a .so containing the
> > OCaml runtime. To do this I needed to patch the runtime (patch is
> > going into 3.11 apparently).
> > http://caml.inria.fr/mantis/view.php?id=3866
>
> Well, should I better wait until release of 3.11 for such kinds of
> projects?!
>
> >
> > - To generate a shared library, you will need to generate PIC code.
> > In recent versions it's the default, but you may need to enable it
> > (ocamlopt -fPIC).
> >
> > - You need to initialize the OCaml runtime.
> [...]
>
> So I have C-code, and from there calling my OCaml-code,
> right? So there is no direct way for it, it seems.
>
> >
> > - Beware of multiple C threads calling into the OCaml code.
> >
> > BTW if you do work it out, *please* post a mini-project or the steps
> > you used on a web page somewhere.
> [...]
Mmmm. It's a long time since I should have done this...
>
> Well, the stuff is not really huge, so I also can write it entirely
> in C. That seems to me to be faster in the over-all process.
>
> But maybe I will try it with OCaml later on.
> Because it's a small lib, this also might be good for
> exploring such kind of OCaml-based library....well,
> we will see how I will decide in the future 
>
> If I possibly make it, I will make some web-description available
> then.
>
> But first I want to have the stuff available
> for myself (I think in some hours I have it ready in C).
If your library is small and could be a good example, I can help
turning it into a .so.
I have some OMake code that helps.
Salutations
Matt
__._,_.___
.
__,_._,___
|
| Re: "ocaml_beginners"::[]
Making a general library from OCaml-code |
  United Kingdom |
2008-06-03 13:07:10 |
|
On Tuesday 03 June 2008 12:51:05 Richard Jones wrote:
> BTW if you do work it out, *please* post a mini-project or the steps
> you used on a web page somewhere.
If I might reiterate this: lots of people want to be able to build DLLs from
OCaml but nobody has ever written a guide explaining how it can be done even
though a couple of people have claimed to be able to do it.
I would also love to see such a guide.
--
Dr Jon D Harrop, Flying Frog Consultancy Ltd.
http://www.ffconsultancy.com/products/?e
__._,_.___
.
__,_._,___
|
| Re: "ocaml_beginners"::[]
Making a general library from OCaml-code |
  United Kingdom |
2008-06-03 13:16:05 |
|
On Tuesday 03 June 2008 13:06:55 Oliver Bandel wrote:
> Well, should I better wait until release of 3.11 for such kinds of
> projects?!
Are you sure this will even be in 3.11?
> Well, the stuff is not really huge, so I also can write it entirely
> in C. That seems to me to be faster in the over-all process.
>
> But maybe I will try it with OCaml later on.
> Because it's a small lib, this also might be good for
> exploring such kind of OCaml-based library....well,
> we will see how I will decide in the future 
>
> If I possibly make it, I will make some web-description available
> then.
>
> But first I want to have the stuff available for myself (I think in some
> hours I have it ready in C).
This kind of thing is vastly easier and more productive with a common language
run-time (assuming your users are also using a high-level language). This is
a major reason to build upon the JVM or Mono and is of particular importance
to commercial developers who want to sell DLLs.
--
Dr Jon D Harrop, Flying Frog Consultancy Ltd.
http://www.ffconsultancy.com/products/?e
__._,_.___
.
__,_._,___
|
| Re: "ocaml_beginners"::[]
Making a general library from OCaml-code |
  United States |
2008-06-03 16:29:46 |
|
Jon Harrop a écrit :
>
> On Tuesday 03 June 2008 12:51:05 Richard Jones wrote:
> > BTW if you do work it out, *please* post a mini-project or the steps
> > you used on a web page somewhere.
> If I might reiterate this: lots of people want to be able to build
DLLs from
> OCaml but nobody has ever written a guide explaining how it can be
done even
> though a couple of people have claimed to be able to do it.
Hum. All I'm doing is following the explanations of chapter 18 of the
manual!
Anyway, here is a stupid example.
******* Here is my Caml code
matt gros:~/projets/so$ cat module1.ml
let hello y = "Hello " ^ y ^ "! I hope everything is ok?"
matt gros:~/projets/so$ cat module2.ml
let s y = Module1.hello y
matt gros:~/projets/so$ cat caml_stub.ml
let _ = Callback.register "my beautiful function" (fun name -> Module2.s
name ^ " (returned by caml_stub)")
******* Let's compile the caml code into my_caml_code.o
matt gros:~/projets/so$ ocamlopt -c module1.ml
matt gros:~/projets/so$ ocamlopt -c module2.ml
matt gros:~/projets/so$ ocamlopt -c caml_stub.ml
matt gros:~/projets/so$ ocamlopt -output-obj -o my_caml_code.o
module1.cmx module2.cmx caml_stub.cmx
******* Now the DLL itself (the caml runtime has to be started
manually. It is possible to have the caml_startup function be
called automatically when the DLL is loaded.)
matt gros:~/projets/so$ cat my_dll.c
#include <stdio.h>
#include <assert.h>
#include <string.h> /* strdup */
#include <caml/alloc.h>
#include <caml/memory.h>
#include <caml/callback.h>
#include <caml/mlvalues.h>
static int my_dll_init_done = 0;
void my_dll_init (void)
{
char *vide[2];
vide[0] = "bof";
vide[1] = NULL;
if (!my_dll_init_done)
{
caml_startup (vide);
my_dll_init_done = 1;
}
}
int my_dll_is_init_done (void)
{
return my_dll_init_done;
}
void my_dll_try_it(char * name){
value res;
char * ch;
static value *cbk = NULL;
if (cbk == NULL)
cbk = caml_named_value("my beautiful function");
assert(cbk);
res = caml_callback(*cbk, caml_copy_string(name));
ch = strdup (String_val(res));
printf("%sn", ch);
free(ch);
}
******* ocamlc knows how to compile C code, and is kind enough to show
us if asked to be verbose
(Maybe I should have used ocamlopt, here, to be coherent)
matt gros:~/projets/so$ ocamlc -verbose -c my_dll.c
+ gcc -fno-defer-pop -Wall -D_FILE_OFFSET_BITS=64 -D_REENTRANT -fPIC
-c -I'/home/matt/godi/lib/ocaml/std-lib' 'my_dll.c'
******* And now link the shared library. Adding the camlruntime: here
the native one is used (asmrun), since we used ocamlopt. The -lm and
-ldl additionnal libraries to link are listed by ocamlc -config
matt gros:~/projets/so$ ocamlc -config
version: 3.10.1
standard_library_default: /home/matt/godi/lib/ocaml/std-lib
standard_library: /home/matt/godi/lib/ocaml/std-lib
standard_runtime: /home/matt/godi/bin/ocamlrun
ccomp_type: cc
bytecomp_c_compiler: gcc -fno-defer-pop -Wall -D_FILE_OFFSET_BITS=64
-D_REENTRANT -fPIC
bytecomp_c_linker: gcc -Wl,-E -L/home/matt/godi/lib
-Wl,-R/home/matt/godi/lib
bytecomp_c_libraries: -lm -ldl -lpthread
native_c_compiler: gcc -Wall -D_FILE_OFFSET_BITS=64 -D_REENTRANT
native_c_linker: gcc -L/home/matt/godi/lib -Wl,-R/home/matt/godi/lib
native_c_libraries: -lm -ldl
native_partial_linker: ld -r
ranlib: ranlib
cc_profile: -pg
architecture: i386
model: default
system: linux_elf
ext_obj: .o
ext_asm: .s
ext_lib: .a
ext_dll: .so
os_type: Unix
default_executable_name: a.out
systhread_supported: true
matt gros:~/projets/so$ gcc -shared -o mydll.so my_dll.o my_caml_code.o | |