Search code examples
autotoolsautoconfautomakepkg-config

proper way to link a library with autotools


I need to link libmagic to my project which is built with autotools.

my current solution is: LIBS+="-lmagic" in configure.ac

but I think that a more proper way to achieve this would be using PKG_CHECK_MODULES macro in configure.ac and LDADD = @MAGIC_LIBS@ in Makefile.am, unfortunately this isn't working.

here is what i'm getting from ./configure

configure: error: Package requirements (magic) were not met:

No package 'magic' found

Consider adjusting the PKG_CONFIG_PATH environment variable if you
installed software in a non-standard prefix.

I think this has something to do with the absence of libmagic in pkg-config, any ideas?


Solution

  • I think that a more proper way to achieve this would be using PKG_CHECK_MODULES macro in configure.ac [...]

    Only if you actually have a pkg-config data file for libmagic, and even then there is some controversy around PKG_CHECK_MODULES. As far as I can determine, the implementation you are probably using does not provide a pkg-config file. You could write your own, of course, but that would defeat the purpose.

    If you're going to rely on the header and library being installed in one of the standard locations -- which is conventional and eminently reasonable -- then a more idiomatic way to configure the build would be to have configure test for their presence. For example,

    AC_CHECK_HEADER([magic.h], [], [
      AC_MSG_ERROR([required header magic.h not found])
    ])
    AC_SEARCH_LIBS([magic_open], [magic], [], [
      AC_MSG_ERROR([required library libmagic not found])
    ])
    

    Note that AC_SEARCH_LIBS() will automatically prepend -lmagic to LIBS if it is found.

    You could get fancier by providing configure options by which a user could specify alternative locations for the header and library, but that doesn't seem to be what you're looking for, and it seems like it would be overkill for libmagic.