Like it!

Join us on Facebook!

Like it!

Recursing subdirectories with Autotools

Automake can recurse into subdirectories and compile source files from there.

Lately I had the need to split the source code of a C++ project into smaller directories, in order to mantain a certain level of organization (and mental sanity as well). Automake supports the so-called make recursion: the ability to recursively parse a list of subdirectories and parse/execute other Makefiles from there. (Un)Fortunately that feature is highly discouraged:

  • it forces you to create a new Makefile.am file per sub-directory (no thanks);
  • it could be harmful (according to this article).

A great alternative is the so-called non-recursive make, simpler and easy to mantain. The game starts from the top-level directory, where lies the one and only Makefile.am. This is the top-level file which contains the options for Automake. Here you have to put references to source files with a relative path, for example:

...
myapp_SOURCES = \
src/folder/core.cpp \
src/folder/utils.cpp \
src/another-folder/core.cpp \
src/another-folder/something.cpp
...

This will compile all the source files in object files directly inside the top build directory. Is that what you want? Not in my case and in the example above, because both objects generated from src/folder/core.cpp and from src/another-folder/core.cpp would clash.

That's not a problem: you just have to tell Automake to leave objects in sub-directories, instead of putting them all in one place. To do so, change the AM_INIT_AUTOMAKE call in configure.ac and add the option subdir-objects:

...
AM_INIT_AUTOMAKE([subdir-objects])
...

And you're done.

Sources

Autotools.io - Non-recursive Automake (link)
David A. Wheeler - Introduction to the Autotools, part 3 (link, video)

comments