automake: Objects created both with libtool and without

1 
1 8.3.9.2 Objects ‘created with both libtool and without’
1 .......................................................
1 
1 Sometimes, the same source file is used both to build a libtool library
1 and to build another non-libtool target (be it a program or another
1 library).
1 
1    Let’s consider the following ‘Makefile.am’.
1 
1      bin_PROGRAMS = prog
1      prog_SOURCES = prog.c foo.c ...
1 
1      lib_LTLIBRARIES = libfoo.la
1      libfoo_la_SOURCES = foo.c ...
1 
1 (In this trivial case the issue could be avoided by linking ‘libfoo.la’
1 with ‘prog’ instead of listing ‘foo.c’ in ‘prog_SOURCES’.  But let’s
1 assume we really want to keep ‘prog’ and ‘libfoo.la’ separate.)
1 
1    Technically, it means that we should build ‘foo.$(OBJEXT)’ for
1 ‘prog’, and ‘foo.lo’ for ‘libfoo.la’.  The problem is that in the course
1 of creating ‘foo.lo’, libtool may erase (or replace) ‘foo.$(OBJEXT)’,
1 and this cannot be avoided.
1 
1    Therefore, when Automake detects this situation it will complain with
1 a message such as
1      object 'foo.$(OBJEXT)' created both with libtool and without
1 
1    A workaround for this issue is to ensure that these two objects get
1 different basenames.  As explained in ⇒Renamed Objects, this
1 happens automatically when per-targets flags are used.
1 
1      bin_PROGRAMS = prog
1      prog_SOURCES = prog.c foo.c ...
1      prog_CFLAGS = $(AM_CFLAGS)
1 
1      lib_LTLIBRARIES = libfoo.la
1      libfoo_la_SOURCES = foo.c ...
1 
1 Adding ‘prog_CFLAGS = $(AM_CFLAGS)’ is almost a no-op, because when the
1 ‘prog_CFLAGS’ is defined, it is used instead of ‘AM_CFLAGS’.  However as
1 a side effect it will cause ‘prog.c’ and ‘foo.c’ to be compiled as
1 ‘prog-prog.$(OBJEXT)’ and ‘prog-foo.$(OBJEXT)’, which solves the issue.
1