Here is another, trickier example. It shows how to generate two
programs (true
and false
) from the same source file
(true.c). The difficult part is that each compilation of
true.c requires different cpp
flags.
bin_PROGRAMS = true false false_SOURCES = false_LDADD = false.o true.o: true.c $(COMPILE) -DEXIT_CODE=0 -c true.c false.o: true.c $(COMPILE) -DEXIT_CODE=1 -o false.o -c true.c
Note that there is no true_SOURCES
definition. Automake will
implicitly assume that there is a source file named true.c
(see Default _SOURCES), and
define rules to compile true.o and link true. The
‘true.o: true.c’ rule supplied by the above Makefile.am,
will override the Automake generated rule to build true.o.
false_SOURCES
is defined to be empty—that way no implicit value
is substituted. Because we have not listed the source of
false, we have to tell Automake how to link the program. This is
the purpose of the false_LDADD
line. A false_DEPENDENCIES
variable, holding the dependencies of the false target will be
automatically generated by Automake from the content of
false_LDADD
.
The above rules won't work if your compiler doesn't accept both -c and -o. The simplest fix for this is to introduce a bogus dependency (to avoid problems with a parallel make):
true.o: true.c false.o $(COMPILE) -DEXIT_CODE=0 -c true.c false.o: true.c $(COMPILE) -DEXIT_CODE=1 -c true.c && mv true.o false.o
As it turns out, there is also a much easier way to do this same task.
Some of the above technique is useful enough that we've kept the
example in the manual. However if you were to build true
and
false
in real life, you would probably use per-program
compilation flags, like so:
bin_PROGRAMS = false true false_SOURCES = true.c false_CPPFLAGS = -DEXIT_CODE=1 true_SOURCES = true.c true_CPPFLAGS = -DEXIT_CODE=0
In this case Automake will cause true.c to be compiled twice, with different flags. In this instance, the names of the object files would be chosen by automake; they would be false-true.o and true-true.o. (The name of the object files rarely matters.)