caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Christian Lindig <lindig@gmail.com>
To: caml-list@inria.fr
Subject: [Caml-list] Using OCamlbuild with C stubs
Date: Mon, 5 Oct 2015 18:03:32 +0200	[thread overview]
Message-ID: <6BA70943-6E5D-4123-92AE-0FCC87628F38@gmail.com> (raw)


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


                 reply	other threads:[~2015-10-05 16:03 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=6BA70943-6E5D-4123-92AE-0FCC87628F38@gmail.com \
    --to=lindig@gmail.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;
as well as URLs for NNTP newsgroup(s).