automake-1.16: Linking

 
 8.1.2 Linking the program
 -------------------------
 
 If you need to link against libraries that are not found by ‘configure’,
 you can use ‘LDADD’ to do so.  This variable is used to specify
 additional objects or libraries to link with; it is inappropriate for
 specifying specific linker flags; you should use ‘AM_LDFLAGS’ for this
 purpose.
 
    Sometimes, multiple programs are built in one directory but do not
 share the same link-time requirements.  In this case, you can use the
 ‘PROG_LDADD’ variable (where PROG is the name of the program as it
 appears in some ‘_PROGRAMS’ variable, and usually written in lowercase)
 to override ‘LDADD’.  If this variable exists for a given program, then
 that program is not linked using ‘LDADD’.
 
    For instance, in GNU cpio, ‘pax’, ‘cpio’ and ‘mt’ are linked against
 the library ‘libcpio.a’.  However, ‘rmt’ is built in the same directory,
 and has no such link requirement.  Also, ‘mt’ and ‘rmt’ are only built
 on certain architectures.  Here is what cpio’s ‘src/Makefile.am’ looks
 like (abridged):
 
      bin_PROGRAMS = cpio pax $(MT)
      libexec_PROGRAMS = $(RMT)
      EXTRA_PROGRAMS = mt rmt
 
      LDADD = ../lib/libcpio.a $(INTLLIBS)
      rmt_LDADD =
 
      cpio_SOURCES = ...
      pax_SOURCES = ...
      mt_SOURCES = ...
      rmt_SOURCES = ...
 
    ‘PROG_LDADD’ is inappropriate for passing program-specific linker
 flags (except for ‘-l’, ‘-L’, ‘-dlopen’ and ‘-dlpreopen’).  So, use the
 ‘PROG_LDFLAGS’ variable for this purpose.
 
    It is also occasionally useful to have a program depend on some other
 target that is not in fact part of that program.  This can be done using
 either the ‘PROG_DEPENDENCIES’ or the ‘EXTRA_PROG_DEPENDENCIES’
 variable.  Each program depends on the contents both variables, but no
 further interpretation is done.
 
    Since these dependencies are associated to the link rule used to
 create the programs they should normally list files used by the link
 command.  That is ‘*.$(OBJEXT)’, ‘*.a’, or ‘*.la’ files.  In rare cases
 you may need to add other kinds of files such as linker scripts, but
 _listing a source file in ‘_DEPENDENCIES’ is wrong_.  If some source
 file needs to be built before all the components of a program are built,
 consider using the ‘BUILT_SOURCES’ variable instead (⇒Sources).
 
    If ‘PROG_DEPENDENCIES’ is not supplied, it is computed by Automake.
 The automatically-assigned value is the contents of ‘PROG_LDADD’, with
 most configure substitutions, ‘-l’, ‘-L’, ‘-dlopen’ and ‘-dlpreopen’
 options removed.  The configure substitutions that are left in are only
 ‘$(LIBOBJS)’ and ‘$(ALLOCA)’; these are left because it is known that
 they will not cause an invalid value for ‘PROG_DEPENDENCIES’ to be
 generated.
 
    ⇒Conditional Sources shows a situation where ‘_DEPENDENCIES’
 may be used.
 
    The ‘EXTRA_PROG_DEPENDENCIES’ may be useful for cases where you
 merely want to augment the ‘automake’-generated ‘PROG_DEPENDENCIES’
 rather than replacing it.
 
    We recommend that you avoid using ‘-l’ options in ‘LDADD’ or
 ‘PROG_LDADD’ when referring to libraries built by your package.
 Instead, write the file name of the library explicitly as in the above
 ‘cpio’ example.  Use ‘-l’ only to list third-party libraries.  If you
 follow this rule, the default value of ‘PROG_DEPENDENCIES’ will list all
 your local libraries and omit the other ones.