How do I specify in a Makefile.am script that I only want to compile object .o files?

Automake cannot build objects without an explicit target (program, library) that will use these objects. One reason is that compilation options are specified per-target. If two targets, e.g. two binaries use the same object but have different compilation option, the same object may have to be compiled twice.

You have three ways to do what you want, they all involve tying your source files to some target.

  1. Do not use a src/ctrnn/Makefile.am, just make reference to the subdirectory source files from your src/Makefile.am:

    bin_PROGRAMS = foo
    foo_SOURCES = main.c crtnn/network.cpp crtnn/neuron.cpp
    
    Note that this will build network.o and neuron.o in the same directory as main.o. If you want objects in subdirectories, use AUTOMAKE_OPTIONS = subdir-objects.

  2. Use a convenience library. In src/crtnn/Makefile.am make a library of your two objects:

    noinst_LIBRARIES = libcrtnn.a
    libcrtnn_a_SOURCES = network.cpp neuron.cpp
    
    and in src/Makefile.am, link your executable to the library:
    bin_PROGRAMS = foo
    foo_SOURCES = main.c
    foo_LDADD = crtnn/libcrtnn.a
    SUBDIRS = crtnn
    
    It's called "convenience" when it is not going to be installed (you can tell because of the noinst_ prefix): it is just used during the build. And this is a static library, so the result is the same as if you had replaced crtnn/libcrtnn.a by crtnn/network.o and crtn/neuro.o when linking foo.

  3. Use a Libtool convenience library. This requires more setup if you are not using Libtool already. You should add a call LT_INIT in configure.ac and rerun autoreconf to install the libtool files. Then you can update src/crtnn/Makefile.am to make a library of your two objects as follows:

    noinst_LTLIBRARIES = libcrtnn.la
    libcrtnn_la_SOURCES = network.cpp neuron.cpp
    
    and src/Makefile.am as follows:
    bin_PROGRAMS = foo
    foo_SOURCES = main.c
    foo_LDADD = crtnn/libcrtnn.la
    SUBDIRS = crtnn
    
    What is the difference? you may ask, almost none. One advantage of using Libtool convenience libraries is that they can be nested: a Libtool library can include another Libtool library (this is convenient when you have a deep hierarchy of source code and you are building a library at each level). Also Libtool convenience libraries can be used to build a shared library if you want. Automake's static libraries cannot.


You could simply specify source files in project/src/Makefile.am and not have a Makefile.am in ctrnn:

maude_SOURCES = ctrnn/network.cpp ctrnn/neuron.cpp

or you can use a libtool convenience library. In ctrnn/Makefile.am, put:

noinst_LTLIBRARIES = libctrnn.la
libctrnn_la_SOURCES = network.cpp neuron.cpp

and in src/Makefile.am, put

LDADD = ctrnn/libmylib.la

If you aren't already using libtool, you'll also need to add LT_INIT to configure.ac.