Search code examples
linuxmakefileautoconf

Autoconf with PKG_CHECK_MODULES doesn't set <prefix>_CFLAGS and <prefix>_LIBS variables


I am trying to compile an application that uses GLIB and DBUS. I have the following configure.ac

AC_INIT([My awesome application], 1.0)
AM_INIT_AUTOMAKE
AC_PROG_CC
AC_CONFIG_FILES(Makefile)
AC_OUTPUT
PKG_CHECK_MODULES([GLIB], glib-2.0 >= 2.0)
PKG_CHECK_MODULES(DBUS, dbus-1 >= 1.0)
PKG_CHECK_MODULES([DBUSGLIB], dbus-glib-1 >= 0.1)
PKG_CHECK_MODULES([GIO], gio-2.0 >= 2.0)
PKG_CHECK_MODULES([GIOUNIX], gio-unix-2.0 >= 2.0)

Then I have the following Makefile.am

bin_PROGRAMS = myapp
myapp_SOURCES = myapp.c
myapp_CFLAGS = ${GLIB_CFLAGS}
myapp_CFLAGS += ${DBUS_CFLAGS}
myapp_CFLAGS += ${DBUSGLIB_CFLAGS}
myapp_CFLAGS += ${GIO_CFLAGS}
myapp_CFLAGS += ${GIOUNIX_CFLAGS}
myapp_LDADD = ${GLIB_LIBS}
myapp_LDADD += ${DBUS_LIBS}
myapp_LDADD += ${DBUSGLIB_LIBS}
myapp_LDADD += ${GIO_LIBS}
myapp_LDADD += ${GIOUNIX_LIBS}

When I run make, none of the ${xxxx_CFLAGS} or ${xxxxLIBS} are included on the gcc command. The compilation fails with a glib.h not found or library missing error.

If I replace each of them in the Makefile.am by the output of pkg-config like this:

bin_PROGRAMS = myapp
myapp_SOURCES = myapp.c
myapp_CFLAGS = -I/usr/include/dbus-1.0 -I/usr/lib/x86_64-linux-gnu/dbus-1.0/include -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include
myapp_CFLAGS += -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include
myapp_CFLAGS += -I/usr/include/dbus-1.0 -I/usr/lib/x86_64-linux-gnu/dbus-1.0/include
myapp_CFLAGS += -pthread -I/usr/include/libmount -I/usr/include/blkid -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include
myapp_CFLAGS += -I/usr/include/gio-unix-2.0 -I/usr/include/libmount -I/usr/include/blkid -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include
myapp_LDADD = -ldbus-glib-1 -ldbus-1 -lgobject-2.0 -lglib-2.0
myapp_LDADD += -lglib-2.0
myapp_LDADD += -ldbus-1
myapp_LDADD += -lgio-2.0 -lgobject-2.0 -lglib-2.0

then it compiles and I can run the application.

All the steps I did:

aclocal
autoconf
automake --add-missing --foreign
./configure
make

Am I missing something or are the variable names not correct? Note I have searched several questions here which use similar build as me and none worked (like @xxxx_CFLAGS@ or $(xxxx_CFLAGS) ...


Solution

  • So at the end I was just missing AC_OUTPUT in my configure.ac file. The end result is:

    AC_INIT([My awesome application], 1.0)
    AC_CONFIG_AUX_DIR(config)
    AM_INIT_AUTOMAKE([foreign])
    LT_INIT
    LT_PREREQ([2.2])
    AC_PROG_CC
    AC_CONFIG_HEADERS([config.h])
    AC_CONFIG_FILES([Makefile])
    AC_CONFIG_MACRO_DIRS([m4])
    PKG_CHECK_MODULES(GLIB, glib-2.0 >= 2.0)
    PKG_CHECK_MODULES(DBUS, dbus-1 >= 1.0)
    PKG_CHECK_MODULES(DBUSGLIB, dbus-glib-1 >= 0.1)
    PKG_CHECK_MODULES(GIO, gio-2.0 >= 2.0)
    PKG_CHECK_MODULES(GIOUNIX, gio-unix-2.0 >= 2.0)
    AC_OUTPUT