caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: <RABIH.ELCHAAR@sgam.com>
To: <ben.aurel@gmail.com>
Cc: <caml-list@inria.fr>
Subject: RE: [Caml-list] Shared libraries with ocamlopt callable from C(without main())?
Date: Fri, 9 Jan 2009 09:44:31 +0100	[thread overview]
Message-ID: <0172C3AE3A0E964797AF43E206C1048F015DD95C@FRCOR-EXMB06.europe.am.socgen> (raw)
In-Reply-To: <49670917.3040103@gmail.com>

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


  reply	other threads:[~2009-01-09  8:47 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-01-08 22:45 Shared libraries with ocamlopt callable from C (without main())? Ben Aurel
2009-01-09  8:21 ` [Caml-list] " Matthieu Dubuget
2009-01-09  8:44   ` RABIH.ELCHAAR [this message]
2009-01-10  1:22   ` Ben Jakb
2009-01-10 11:50     ` Matthieu Dubuget
2009-01-10 15:28       ` Ben Jakb
2009-01-10 15:36         ` Matthieu Dubuget
2009-01-10 18:08           ` Ben Jakb
     [not found]       ` <200901101911.41594.monnier.florent@gmail.com>
2009-01-10 18:18         ` Ben Jakb
2009-01-11 16:27     ` [Caml-list] " Xavier Leroy

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=0172C3AE3A0E964797AF43E206C1048F015DD95C@FRCOR-EXMB06.europe.am.socgen \
    --to=rabih.elchaar@sgam.com \
    --cc=ben.aurel@gmail.com \
    --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).