English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

Browse thread
Linking to C libraries when calling camlp4
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2010-01-29 (09:34)
From: Joseph Young <ocaml@o...>
Subject: Linking to C libraries when calling camlp4
 	How do you link to external C libraries when calling camlp4?  When 
calling ocamlc, we can use the -custom and -cclib flags in order to 
statically link a library.  However, the camlp4 executable seems to ignore 
these flags when called.  As an example, given the files:


$ cat Makefile
         gcc -c myadd.c
         ocamlc -c myadd.ml
         ocamlmktop -custom -cclib myadd.o -o mytop myadd.cmo
         ocamlc -c -I +camlp4 -I +camlp4/Camlp4Parsers -pp camlp4of 

         rm *.cmo
         rm *.cmi
         rm *.o

$ cat myadd.c
int add1(int x){
         return x+1;

$ cat myadd.ml
external add1 : int->int = "add1";;

$ cat myquot.ml
module CamlSyntax = Camlp4OCamlParser.Make(

open Camlp4.PreCast;;

let expr_of_string = CamlSyntax.Gram.parse_string CamlSyntax.expr_eoi;;
module MyGram = Camlp4.PreCast.MakeGram(Camlp4.PreCast.Lexer);;

let term = MyGram.Entry.mk "term";;
let term_eoi = MyGram.Entry.mk "term quotation";;

     GLOBAL: term term_eoi;
     [  "simple"
         [  `INT(i,_)  -> <:expr< $`int:Myadd.add1 i$ >> ]
     [[ t = term; `EOI -> t ]];

let expand_quot loc loc_opt s = MyGram.parse_string term_eoi loc s;;

Syntax.Quotation.add "abc" Camlp4.PreCast.Syntax.Quotation.DynAst.expr_tag

$ cat test.ml
let x= <:abc< 4 >>;;


If we call:

$ ./mytop dynlink.cma camlp4of.cma myquot.cmo
         Objective Caml version 3.11.2

         Camlp4 Parsing version 3.11.2

# let x= <:abc< 4 >>;;
val x : int = 5

everything works fine since mytop has been compiled with the correct C 
object file.  However, if we try the following call:

$ camlp4of myadd.cmo myquot.cmo test.ml
Camlp4: Uncaught exception: DynLoader.Error ("./myquot.cmo", "error while 
linking ./myquot.cmo.\nThe external function `add1' is not available")

we have an error since the C object file has not been linked.  How do we 
correctly preprocess the file test.ml?