Writing Free Software – Part 10: Subdirectories

In this installment, I’ll cover creating different subdirectories for different parts of the code. We’ll move NDesk.Options.cs into an ndesk directory and Greeting.cs into a src directory. Some modifications need to be made to various pieces of the distribution in order for this to work out correctly. We’ll do the minimum required now and cover the pieces that will make it more extensible but complex in future entries.

Return to the workplace

$ cd ~/src/greeting

Move the source to their new directories

$ mkdir src ndesk
$ mv NDesk.Options.cs ndesk
$ mv Greeting.cs src
$ mv Makefile.am src

Create a new root Makefile.am file

$ cat > Makefile.am
SUBDIRS = src ndesk

bin_SCRIPTS = greeting

Create ndesk/Makefile.am

$ cat > ndesk/Makefile.am
EXTRA_DIST = NDesk.Options.cs

Alter src/Makefile.am to cope with new layout

$ patch src/Makefile.am

< Greeting.exe: Greeting.cs NDesk.Options.cs
< # the same as $(CSC) -out:Greeting.exe Greeting.cs
> Greeting.exe: Greeting.cs $(top_srcdir)/ndesk/NDesk.Options.cs
> # the same as $(CSC) -out:Greeting.exe Greeting.cs ...
< EXTRA_DIST = Greeting.cs NDesk.Options.cs
> EXTRA_DIST = Greeting.cs
< bin_SCRIPTS = greeting
patching file src/Makefile.am

Alter configure.ac to cope with new layout

$ patch configure.ac

@@ -13,6 +13,8 @@
missing header for unified diff at line 1 of patch
patching file configure.ac

Prove that we didn’t break anything

$$ automake && autoconf && ./configure
$ make
$ sudo make install
$ greeting
Greetings, World!


In this lesson, I gave an example of re-factoring the build system to use subdirectories. This method can be used to separate portions of one’s codebase. In addition, we saw the use of the $srcdir variable, which stores the location of the root of the source distribution.

In future posts, we will use the work done in this lesson as a basis for some more re-factoring. We will also learn how to do things such as

  • creating sub projects
  • inter-package dependencies using pkg-config
, , , , , ,

4 responses to “Writing Free Software – Part 10: Subdirectories”

  1. Stephen,

    This seems reasonable to me. Jon Pryor also mentioned this and pointed me toward your link and the pdf version at


    I think this sort of collapsing of recursive Makefile files into a single authoritative file should be handled by automake, however, and not manually tackled by the Makefile.am writer. For the sake of simplicity, I will leave the SUBDIRS section as it is and recommend that readers take a look at the links mentioned by you and Jon.



Leave a Reply