Search code examples
autotoolsautoconfautomakepkg-config

autoconf-generated Makefile does not pass flags for library headers when using PKG_CHECK_MODULES


My project depends upon a library (more precisely, GTK+) so I added the following configurations in my configure.ac:

PKG_CHECK_MODULES([GTK], [gtk+-2.0])
AC_SUBST([GTK_CFLAGS])
AC_SUBST([GTK_LIBS])

My Makefile.am is:

bin_PROGRAMS = secretary
secretary_SOURCES = secretary.c

For its turn, my secretary.c is as follows:

#include <gtk/gtk.h>

int main(int argc, char *argv[]) {
    gtk_init(&argc, &argv);
    GtkWidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_widget_show(window);
    gtk_main();
    return 0;
}

However, when I run make (of course, after calling ./configure) I got this error:

gcc -DHAVE_CONFIG_H -I. -g -O2 -MT secretary.o -MD -MP -MF \
   .deps/secretary.Tpo -c -o secretary.o secretary.c
secretary.c:1:21: fatal error: gtk/gtk.h: File or directory not found.

What am I missing? Why does autoconf not pass the correct flags to gcc?


Solution

  • When you use PKG_CHECK_MODULES, you need to specify the flags in Makefile.am. The easiest way is to add it to AM_LDFLAGS and AM_CPPFLAGS:

    AM_LDADD = @GTK_LIBS@
    AM_CPPFLAGS = @GTK_CFLAGS@
    

    If you want to be more specific, you can instead add:

    secretary_LDADD = @GTK_LIBS@
    secretary_CPPFLAGS = @GTK_CFLAGS@
    

    It is probably easier to not use PKG_CHECK_MODULES at all and let the user specify the location of the libraries through the usual mechanism (assigning LDFLAGS or installing the libraries in a standard location).