caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Linking to C libraries when calling camlp4
@ 2010-01-29  9:12 Joseph Young
  0 siblings, 0 replies; only message in thread
From: Joseph Young @ 2010-01-29  9:12 UTC (permalink / raw)
  To: caml-list

Hi,
 	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
all:
         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 
myquot.ml

clean:
         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(
     Camlp4OCamlRevisedParser.Make(Camlp4.PreCast.Syntax));;

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";;


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

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
     expand_quot;;

$ 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?

Joe


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2010-01-29  9:34 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-01-29  9:12 Linking to C libraries when calling camlp4 Joseph Young

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).