vinllen
vinllen

Reputation: 1459

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

Upvotes: 0

Views: 1690

Answers (2)

umläute
umläute

Reputation: 31284

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)

Upvotes: 1

Tim
Tim

Reputation: 1933

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

Upvotes: 0

Related Questions