Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

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: -- (:)
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
         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
external add1 : int->int = "add1";;

$ cat
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 = "term";;
let term_eoi = "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
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
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