0

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

Community
  • 1
  • 1
vinllen
  • 1,369
  • 2
  • 18
  • 36

2 Answers2

1

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)
umläute
  • 28,885
  • 9
  • 68
  • 122
0

The makefile itself doesn't care about your code, compiled or not. It's the compiler job.

If you use GCC you can define symbols with the -D options like this :

gcc -DABC=3 -o main.o main.c

You can of course use a Makefile variable like this :

DEFINED_MACRO = ABC
target.o: file.c
    gcc -D$(DEFINED_MACRO)=3 -o target.o file.c
Tim
  • 1,853
  • 2
  • 24
  • 36