caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Using OCamlbuild with C stubs
@ 2015-10-05 16:03 Christian Lindig
  0 siblings, 0 replies; only message in thread
From: Christian Lindig @ 2015-10-05 16:03 UTC (permalink / raw)
  To: caml-list


What is the recommended way to use OCamlbuild when implementing a few primitives in C?
I’ve seen several blog posts about this and I feel there are some struggles. 

* https://ocaml.org/learn/tutorials/ocamlbuild/Ocamlbuild_example_with_C_stubs.html

This is quite involved and requires experience with customizing OCamlbuild using the myocamlbuild.ml mechanism.


* http://l-lang.blogspot.de/2012/08/incorporating-c-code-in-ocaml-project.html

This approach implements a minimal new parameterised tag to express that the C object code needs to be linked into the final binary. I had trouble getting it to work when the project spans several directories. But if it works, maybe it should be a built-in tag?

* http://mancoosi.org/~abate/ocamlbuild-and-c-stubs

This strikes a middle ground.

Maybe the projects are so different that there is no one way. I believe one core problem is to express that an object file or local C library needs to be linked into the binary. 



During experiments I came up with an approach that doesn’t require myocamlbuild.ml but seems like a hack: 

$ ocamlbuild -I src -I lib -lflag lib/libpath.a libpath.a main.byte

Building libpath.a is triggered by naming it as a target on the command line; likewise, linking it to the binary main.byte is expressed using the linker flag “-lflag”. This works around the problem that a built-in mechanism seems to be missing to express this dependency using tags.

Project layout:

./_tags
./lib/libpath.clib 		# lists pathutil.o
./lib/pathutil.c 		# new primitive in C
./lib/pathutil.mli 		# interface
./src/main.ml

_tags file:

<**/main.byte>:           custom

You can find it as examples/06-simple-c in my clone of the ocamlbuild manual that Gabriel Scherer is working on: https://github.com/lindig/manual-ocamlbuild.

I understand that dependencies among C files need to be handled as well but would solve this problem outside of Ocamlbuild.


— Christian


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

only message in thread, other threads:[~2015-10-05 16:03 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-10-05 16:03 [Caml-list] Using OCamlbuild with C stubs Christian Lindig

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