2

please excuse me if this question has already been answered elsewhere, but I'm not sure what to search for.

I am passing a value from a batch file, though a makefile, and to a header file, like so:

(Using AVR-GCC 4.5.2)

Batch:

make ADDR=FOO

Makefile:

CFLAGS += -DADDR=$(ADDR)
...
gcc $(CFLAGS) main.c

This is the header file main.h, included by main.c; it is supposed to select a specific pinout at compile-time, based on the value of ADDR:

#include "defs.h"

#if ADDR == FOO
...     // Select pinout 1
#elif ADDR == BAR
...     // Select pinout 2
#endif

The comparison values FOO and BAR are constants defined in a global header file defs.h included by main.h:

#define FOO 23
#define BAR 42

Now the problem: The comparisons don't work, ADDR constantly reads as 0. This seems to be because the preprocessor does not resolve ADDR before the comparison, i.e. expand FOO to 23. Telling the compiler to do this expansion would be helpful.

Of course it works if I pass ADDR=23 directly in the batch file, but for flexibility I want to use the aliases from defs.h instead of fixed numeric constants.

By the way, the following works if placed in main.c:

int addr_val = ADDR;    // is now 23

Maybe there is a simple solution I am not aware of right now. Please feel free to contribute.

Have a nice day!

pnuts
  • 58,317
  • 11
  • 87
  • 139

0 Answers0