title: autoconf/automake: conditional compilation based on presence of library?
body: <p>I need to conditionally compile some code based on the presence of a library.  Seems like this should be easy with autoconf/automake but I can't figure it out.</p>
  
  <p>For example, if there is a PNG library present, I want to include code to use it.  My configure.ac has:</p>
  
  <pre><code>AC_CHECK_LIB([png], [png_create_write_struct_2])
  </code></pre>
  
  <p>and my Makefile.am has:</p>
  
  <pre><code>if USE_LIBPNG
  libdev_la_SOURCES += png.c
  endif
  </code></pre>
  
  <p>(which adds png.c to the list of sources for libdev so it gets compiled).</p>
  
  <p>An automake conditional like USE_LIBPNG requires the conditional be defined in configure.ac, so i need:</p>
  
  <pre><code>AM_CONDITIONAL([USE_LIBPNG], [test SOMETHINGOROTHER])
  </code></pre>
  
  <p>The question is, what can test SOMETHINGOROTHER be?  What does AC_CHECK_LIB define that I can test for?</p>
  
  <p>AC_CHECK_LIB's default behavior is to define a symbol (in config.h) which can be used in source code, but that doesn't help the Makefile since the AM_CONDITIONAL needs a <em>shell test</em></p>
  
  <p>I tried overriding the default AC_CHECK_LIB behavior like so:</p>
  
  <pre><code>AC_CHECK_LIB([png], [png_create_write_struct_2], [HAS_LIBPNG=1])
  </code></pre>
  
  <p>after which I could test for it:</p>
  
  <pre><code>AM_CONDITIONAL([USE_LIBPNG], [test "x$HAS_LIBPNG" = "x1"])
  </code></pre>
  
  <p>This is ugly, but works for the Makefile... but creates a new problem: since it discards the original AC_CHECK_LIB behavior, and I no longer get a symbol added to config.h, which I need.</p>
  
  <p>I must be missing something basic, or possible Doing It Wrong.  Have been digging around for hours and found no answer.</p>
  
  <p>Anyone?</p>