Search code examples
makefilemacrosautomake

how to set macro in makefile.am that i can used in code


How to set macro in makefile.am that i can used in code, e.g., #ifdef ABC, where the ABC variable is defined in makefile.am but can used in code. I have read this question, which is talk about how to set macro in the makefile but not makefile.am


Solution

  • a Makefile.am is a template for a Makefile (well, for Makefile.in which is a template for the final Makefile). While automake will generate quite a lot of code in the Makefile.am -> Makefile.in translation, it will leave parts untouched, allowing you to insert your own make-code.

    a typical (demo) Makefile.am would look like:

    bin_PROGRAMS = foo
    foo_SOURCES = xyz.c
    foo_CPPFLAGS = -DFOO
    

    which will have FOO defined (it adds '-DFOO' to the preprocessor flags) when compiling the foo program.

    a more complex (and unusual) example could look like:

    bin_PROGRAMS = foo
    foo_SOURCES = xyz.c abc.c
    foo_CPPFLAGS = -D@FOODEF@
    

    with a configure.ac that contains something like:

    FOODEF=KNURK
    AC_SUBST(FOODEF)
    

    which provides the equivalent to having #define KNURK in each source-file for foo

    the above is atypical, as usually you would replace "self-contained" flags, e.g. something like, the following Makefile.am:

    bin_PROGRAMS = foo
    foo_SOURCES = xyz.c abc.c
    foo_CPPFLAGS = @FOODEFS@ -I/usr/include/fu/
    

    accompanied with a configure.ac snippet like:

    FOODEFS=
    AC_ARG_WITH([knork], AC_HELP_STRING([--with-knork=<str>], [build with knork (or frozz otherwise]))
    AS_IF([test "x$with_knork" = "xyes" ],FOODEFS="-DKNORK")
    AS_IF([test "x$with_knork" = "xno" ], FOODEFS="-DFROZZ")
    AC_SUBST(FOODEF)