From: Joseph Young <ocaml@optimojoe.com>
To: caml-list@inria.fr
Subject: Linking to C libraries when calling camlp4
Date: Fri, 29 Jan 2010 10:12:44 +0100 (CET) [thread overview]
Message-ID: <Pine.LNX.4.64.1001290952580.4721@myhome> (raw)
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
reply other threads:[~2010-01-29 9:34 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
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=Pine.LNX.4.64.1001290952580.4721@myhome \
--to=ocaml@optimojoe.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