Sharing code: convenience libs

If you are compiling the same source file with the same option into two shared libraries, or into two programs, which are in the same directory, and using the same compilation options, then that's fine, you can list it in both _SOURCES lines. It will in fact be compiled only once, and the object file will be used by both targets.

However, if you are using the same source file in a lib and a program, or if you are sharing it between different directories, then you can't list it in both _SOURCES lines. This is because a lib and a program need different compilation options (-fPIC), and in case of different directories, because automake doesn't support source files in other dirs. Instead, you should put the shared source files in either a shared library (installed) or in a convenience library. The latter is a static library (*.a on Unix), which isn't installed as is. The targets (shared libs or programs) that "link" to the convenience library will in fact incorporate the object files from it.

To define a convenience library:

noinst_LTLIBRARIES = libcommon.la
libcommon_la_SOURCES = dirk.cpp coolo.cpp ...
# no need for LIBADD or LDFLAGS, strictly speaking, but it can help
# if e.g. this code needs $(LIBJPEG), all users of this convenience lib won't have
# to specify it.

# Then you can use the convenience lib:
mylib_la_LIBADD = libcommon.la
myprogram_LDADD = libcommon.la

This is also the way to compile source files in subdirectories. Automake does NOT allow

foo_SOURCES = subdir/bar.cc
you have to define a convenience library inside subdir, and use it from the toplevel.