3
votes

After reading the autotools mythbuster, I have tried to write a little example of the non recursive makefile with the use of subdir-objects in order to have my binary in the directory of my source files.

Here is the the organization of my little test:

/
   autogen.sh
   configure.ac
   Makefile.am
   src/
      main.c

The autogen.sh :

#!/bin/sh
echo "Running aclocal..." ; aclocal $ACLOCAL_FLAGS || exit 1
echo "Running autoheader..." ; autoheader || exit 1
echo "Running autoconf..." ; autoconf || exit 1
echo "Running automake..." ; automake --add-missing --copy --gnu || exit 1
./configure "$@"

The configure.ac :

AC_PREREQ([2.69])
AC_INIT([test], [0.0.1], [[email protected]])
AC_CONFIG_SRCDIR([src/main.c])
AC_CONFIG_HEADERS([config.h])

# Checks for programs.
AC_PROG_CC
AM_INIT_AUTOMAKE([1.14 foreign subdir-objects])
AM_MAINTAINER_MODE([enable])
AC_OUTPUT(Makefile)

The Makefile.am :

MAINTAINERCLEANFILES = Makefile.in aclocal.m4 config.h.in configure depcomp install-sh missing compile
bin_PROGRAMS=toto
toto_SOURCES=src/main.c

I compile everything with :

./autogen.sh
make

I thought that the binary toto would have been created in the src directory with the option subdir-objects but it seems that this option have no effect and the toto binary is always generated in the root directory.

I have also tried to pass this option in the Makefile.am with AUTOMAKE_OPTIONS = subdir-objects but wihtout success.

1
make will automatically look for a file named Makefile or makefile or Makefile.mak or makefile.mak. It will not look for a file name Makefile.am. so use make -f Makefile.amuser3629249
make -f Makefile.am will not work. The Makefile is generated from the Makefile.am by Automake and configure.ptomato

1 Answers

3
votes

That's not what the subdir-objects option does. It puts intermediate build results, especially *.o object files, in the same directory as the sources from which they are built. In particular, you should find main.o there instead of in the top directory.

The final build result, on the other hand, must be what and where you specify in your Automake file. If you want toto to go in the src/ directory too, then use this Makefile.am:

MAINTAINERCLEANFILES = Makefile.in aclocal.m4 config.h.in configure depcomp install-sh missing compile
bin_PROGRAMS=src/toto
src_toto_SOURCES=src/main.c