Version française
Home     About     Download     Resources     Contact us    
Browse thread
Shared libraries with ocamlopt callable from C (without main())?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: RABIH.ELCHAAR@s...
Subject: RE: [Caml-list] Shared libraries with ocamlopt callable from C(without main())?
The simplest way of achieving what you want might be the following:
1) Write the ocaml code you want to call.
2) register your caml functions via Callback.register
3) In your C code, define the callback values for each ocaml function, via a macro like
#define ACTION(actionname)               \
static value *actionname##_closure = NULL;
and fore each c function that wraps the caml call, call ACTION("camlfunctionname") before your c wrapping code.
4) define an initialization helper (to get the pointers to ocaml functions), something like
#define INITACTION(actionname)                             \
    actionname##_closure = caml_named_value(#actionname);
5) In your DLLMain function, you can have something like
BOOL APIENTRY DllMain(HANDLE module, DWORD reason, void *reserved)
{
  char * argv[2];
  switch (reason) {
  case DLL_PROCESS_ATTACH:
    argv[0] = "--";
    argv[1] = NULL;
    caml_startup(argv);
    INITACTION(analyze)
    INITACTION(updateBlob)
    .....
    break;
  }
  return TRUE;
} 

This handles the C functions:(C Wrapper | C API of OCaml ) -> OCaml defining a C dll with the needed entries.

You can use afterwards your langX -> ( C API of langX -> call above dll).
If your langX can call directly C functions without the need of type conversions, this can do.
If not, this approach might give you two dlls (langX -> C dll) and (C -> ocaml) dll.

If you want only one dll, I think this is only a linking issue, and can be solved by inputing the C->ocaml , compiled ocaml and langX->C together to the linker.

Hope this helps,

Sincerely,
Rabih Chaar

-----Message d'origine-----
De : caml-list-bounces@yquem.inria.fr [mailto:caml-list-bounces@yquem.inria.fr] De la part de Matthieu Dubuget
Envoyé : vendredi 9 janvier 2009 09:22
À : caml-list@inria.fr
Objet : Re: [Caml-list] Shared libraries with ocamlopt callable from C(without main())?

Ben Aurel a écrit :
> I'm thinking about the possibility to use OCaml as an extension
> language. If I wanted to call a OCaml function from langX I had to use
> a C wrapper.
>
>     langX -> ( C API of langX | C Wrapper | C API of OCaml ) -> OCaml
>   

Yes

> But could somebody tell me if ( and maybe how ) this can be done. I made some attempts, but
> there is always a main() function involved. But this C wrapper would be a shared library.
>   

Exactly. You can deliver your OCaml code as shared library, and then
call it from
any language that can call a shared library.

Could you point out what problem you faced?

Salutations

Matt


_______________________________________________
Caml-list mailing list. Subscription management:
http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
Archives: http://caml.inria.fr
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs
This message and any attachments (the "message") are confidential, intended solely for the addressee(s), and may contain legally privileged information. 
Any unauthorised use or dissemination is prohibited. 
E-mails are susceptible to alteration. 
Neither Societe Generale Asset Management nor any of its subsidiaries or affiliates shall be liable for the message if altered, changed or falsified. 
  
Find out more about Societe Generale Asset Management's proposal on www.sgam.com
  
                                ******** 
  
Ce message et toutes les pieces jointes (ci-apres le "message") sont confidentiels et susceptibles de contenir des informations couvertes par le secret professionnel.
Ce message est etabli a l'intention exclusive de ses destinataires.
Toute utilisation ou diffusion non autorisee est interdite. 
Tout message electronique est susceptible d'alteration. Societe Generale Asset Management et ses filiales declinent toute responsabilite au titre de ce message s'il a ete altere, deforme ou falsifie. 

Decouvrez l'offre et les services de Societe Generale Asset Management sur le site www.sgam.fr