Using autoconf for OCaml projects

Today Zack and I released the first version of ocaml-autoconf, which is a collection of OCaml-related autoconf macros. That package won’t tell you how to use autoconf though, and that is something that people wrongly regard as being incomprehensible or too hard to learn. In this tutorial you’ll see that you only need to create two files to start even a complex autoconf / OCaml project.

The download which goes along with this tutorial is: ocaml-autoconf-example-1.0.tar.gz

The first file controls autoconf. It is called and looks like this. Notice the use of the OCaml autoconf macros which are documented in the ocaml.m4 manual page.



if test "$OCAMLC" = "no"; then
  AC_MSG_ERROR([You must install the OCaml compiler])

if test "$OCAMLFIND" = "no"; then
  AC_MSG_ERROR([You must install OCaml findlib (the ocamlfind command)])

if test "$OCAML_PKG_lablgtk2" = "no"; then
  AC_MSG_ERROR([Please install OCaml findlib module 'lablgtk2'.])


The second file controls automake. In fact we’re not really using automake’s capabilities, so our looks almost like an ordinary Makefile:

bin_SCRIPTS = gtk_test$(EXEEXT)

PACKAGES = -package lablgtk2
LIBS     = gtkInit.cmo -linkpkg

gtk_test$(EXEEXT): gtk_test.cmo
	$(OCAMLFIND) ocamlc $(PACKAGES) $(LIBS) $^ -o $@

	ocamlfind ocamlc $(PACKAGES) -c $< -o $@


CLEANFILES = *.cmi *.cmo $(bin_SCRIPTS) *~


Those are the only two files you have to write, but you will also need to copy the OCaml autoconf macros (ocaml.m4) into an m4/ subdirectory:

mkdir m4
cp /usr/share/aclocal/ocaml.m4 m4/


Finally run autoreconf to run autoconf and automake and generate the ./configure script. Because automake defaults to assuming your project conforms to all the GNU coding standards, you probably want to add the automake --foreign flag, so:

AUTOMAKE='automake --foreign' autoreconf -i


Now you’re done. You can copy the program into this directory, and try:

make dist
make install


Download: ocaml-autoconf-example-1.0.tar.gz

Update #1: Zack’s blog posting about ocaml-autoconf

Update #2: See comments.

About these ads


Filed under Uncategorized

9 responses to “Using autoconf for OCaml projects

  1. Do you have a example using camlp4?

  2. rwmj

    Andre: It depends what particular syntax extension(s) one is using. The easiest thing to do is to add the appropriate -package and -syntax parameters to the ocamlfind command line. You can find examples here and here.

  3. Thanks for the post. It is always good to have a quick getting started example. The could use some improving.
    As it stands here you cannot ‘make dist’. This is because automake assumes the source files to build the program are in C. The target to build that binary just happen to work because we have overwritten it.
    I have this in my

    FINDLIB_FLAG = -package unix -linkpkg
    bin_PROGRAMS = myprog
    myprog_SOURCES =

    SUFFIXES = .ml

    $(OCAMLFIND) opt $(FINDLIB_FLAG) -c $^ -o $@

    LINK = $(OCAMLFIND) opt $(FINDLIB_FLAG) $^ -o $@

    CLEANFILES = *.cmi *.cmx *.annot *.cmo *~

  4. I’m not familiar with autoconf. Granted, this post is not an autoconf tutorial. However, I’ve been using the macros as explained in this post, with a prior aclocal -I m4 for my m4/ocaml.m4 file. I get error: possibly undefined macro: AC_PROG_OCAML

    Could you quickly explain what to do? If I get this error, no doubt other autocof noobs will encounter when trying to apply this tutorial post.

    • rich

      There could be several reasons for this. I think most likely you’ve missed out this line from


      or this one in


      • Indeed. This solves the problem. However, the problem with autoconf is not autoconf itself, but rather the workflow it imposes with, say, git.

        When you need to make clean, it’s possible that you do not have your Makefile available. Regenerating the Makefile each time you compile your stuff can take a while… By the way, you distribute the, while keeping only the under source control.

        How do you guys cope with all these little annoyances?

      • rich

        I’m not going to pretend that autoconf is good. But for the Fedora cross-compiler project I had to cross-compile about a hundred packages using all sorts of different build systems, and autoconf/automake-based ones were by far the most reliable. Autoconf isn’t good, but it’s much better than the alternatives once you start needing to compile on many different platforms, cross compile, or integrate with Linux distro packaging systems.

  5. maalosh

    Wow ! I’m finding this great tool just now. I have only one question : how do you use autoconf to install a library so that ocamlfind can use it ?


    • rich

      You need to write a META file, put everything in a subdirectory of the OCaml directory, and it should just work. If I were you, I’d take a look at some existing library, eg. /usr/lib*/ocaml/pcre/*

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s