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.
For example, if there is a PNG library present, I want to include code to use it. My configure.ac has:
AC_CHECK_LIB([png], [png_create_write_struct_2])
and my Makefile.am has:
if USE_LIBPNG
libdev_la_SOURCES += png.c
endif
(which adds png.c to the list of sources for libdev so it gets compiled).
An automake conditional like USE_LIBPNG requires the conditional be defined in configure.ac, so i need:
AM_CONDITIONAL([USE_LIBPNG], [test SOMETHINGOROTHER])
The question is, what can test SOMETHINGOROTHER be? What does AC_CHECK_LIB define that I can test for?
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 shell test
I tried overriding the default AC_CHECK_LIB behavior like so:
AC_CHECK_LIB([png], [png_create_write_struct_2], [HAS_LIBPNG=1])
after which I could test for it:
AM_CONDITIONAL([USE_LIBPNG], [test "x$HAS_LIBPNG" = "x1"])
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.
I must be missing something basic, or possible Doing It Wrong. Have been digging around for hours and found no answer.
Anyone?
Best Answer
If the library you're checking for supplies a
.pc
file for use withpkg-config
, then you're much better off usingPKG_CHECK_MODULES
to get the correct flags. libpng does:(in
configure.ac
)This gives you access to the variables
$(libpng_CFLAGS)
and$(libpng_LIBS)
which you will want to add toMakefile.am
(probably inAM_CFLAGS
/AM_CXXFLAGS
andLDADD
, or target-specific versions thereof).It will also cause
configure
to fail with an error iflibpng12.pc
isn't found. If you wantconfigure
to continue, you'll need to supply the third and fourth arguments toPKG_CHECK_MODULES
, which areACTION-IF-FOUND
andACTION-IF-NOT-FOUND
:(in
configure.ac
)Now, if you need an
automake
conditional, you can do something like:(in
configure.ac
)If you also need the preprocessor definition, you could use
AC_DEFINE
like so:(in
configure.ac
)Possibly nicer is to set the definition in
Makefile.am
:(in
Makefile.am
)This will clutter your command line, though, whereas
AC_DEFINE
can put the definition in a header if you useAC_CONFIG_HEADERS
. I guess this doesn't really matter if you useAM_SILENT_RULES([yes])
or don't care about your command line being neat (and let's be honest,automake
generates some pretty gnarly command lines anyway).A note on good
autoconf
styleIt is considered poor form to build optional support based on whether or not a check succeeded (see this gentoo doc for details). Here's how I'd code optional support for libpng:
(in
configure.ac
)(in
Makefile.am
)If your library doesn't have a
.pc
fileFor completeness, here's how I'd check for a library that didn't have a
.pc
file. I'll skip over the details of following goodautoconf
style.AC_CHECK_LIB
sets a cache variable, so you can test that instead of replacing theACTION-IF-FOUND
ofAC_CHECK_LIB
:(in
configure.ac
)IMHO, you should only do it this way if you have no other option.