Search code examples
cconfigureautoconfguile

how to configure.ac with optional GNU guile library ("--with-guile" )


I'm new to configure.ac, I'm trying to create a simple program that would only use the GNU guile library if the user invokes:

configure --with-guile

so the program would be something like:

#include "config.h"
#include <stdio.h>

#ifdef HAVE_GUILE
#include <libguile.h>
#endif

int main(int argc,char** argv) {
#ifdef HAVE_GUILE
    printf("Guile supported\n");
    scm_init_guile();
#else
    printf("Guile not supported\n");
#endif
return 0;
}

GNU guile uses guile-config compile and guile-config link to obtain the path to the include directory and the libraries. I've not found a tutorial where the paths above are obtained via an external program.

So far my `configure.ac` is 


AC_INIT(program, 1.0)


AC_PROG_CC
AC_CONFIG_HEADERS(config.h)

with_guile=no

AC_ARG_WITH(guile, [AS_HELP_STRING([--with-guile], [use gnu guile])],[],[with_guile=yes])


if test "x$with_guile" != no
then
    AC_MSG_CHECKING(for Guile)
    guile-config link > /dev/null || {
        echo "configure: cannot find guile-config; is Guile installed?" 1>&2
        exit 1
      }
    CFLAGS="$CFLAGS `guile-config compile`"
    LDFLAGS="$LDFLAGS `guile-config link`"
    AC_DEFINE([HAVE_GUILE],[1],[Guile supported])

    #PKG_CHECK_MODULES([GUILE],[guile-2.0])
    #AC_CHECK_HEADERS([libguile.h], [], [AC_MSG_ERROR([guile include files not found])])
    #AC_CHECK_LIB([guile], [scm_with_guile], [AC_MSG_ERROR([guile library files not found])])
fi


dnl Process Makefile.in to create Makefile

AC_OUTPUT(Makefile)

I've removed AC_CHECK_HEADERS and AC_CHECK_LIB because they doesn't work (the files are not found).

Here I'm lost: how can I add the guile paths to CFLAGS and LDFLAGS, how can I generate HAVE_GUILE in config.h

current Makefile.in:

CC=@CC@
LD=@CC@
program: program.o
    $(LD) -o $@ $^
.PHONY: clean
clean:
    rm -f program *.o

Solution

  • configure.ac:

    AC_PROG_CC
    AC_CONFIG_HEADERS([config.h])
    AC_ARG_WITH(guile, [AS_HELP_STRING([--with-guile], [use gnu guile])],
               [with_guile=yes],[with_guile=no])
    
    AS_IF([test "x$with_guile" = xyes],
       [AC_CHECK_PROG([GUILE_CONFIG],[guile-config],[guile-config])
        AS_IF([test -z "$GUILE_CONFIG"],[AC_MSG_FAILURE([cannot find guile-config])])
        CFLAGS="$CFLAGS `$GUILE_CONFIG compile`"
        LDFLAGS="$LDFLAGS `$GUILE_CONFIG link`"
        AC_DEFINE([HAVE_GUILE],[1],[Guile supported])])
     AC_CONFIG_FILES([Makefile])
     AC_OUTPUT
    

    is more typical. I used a Makefile.am instead of what you did:

    Makefile.am:

    bin_PROGRAMS = program
    program_SOURCES = src/main.c
    

    which is more typical also.

    config.h has:

    /* config.h.  Generated from config.h.in by configure.  */
    /* config.h.in.  Generated from configure.ac by autoheader.  */
    
    /* Guile supported */
    #define HAVE_GUILE 1
    ...
    

    Makefile has:

    ...
    CFLAGS = -g -O2 -pthread -I/usr/include/guile/2.0 
    ...
    LDFLAGS =  -lguile-2.0 -lgc