Re: Preprocessor directive including another one
"Stefano Sabatini" <stefano.sabatini@caos.org> wrote in message
news:slrng3so3t.87g.stefano.sabatini@geppetto.reil abs.com...
> Hi all C speakers,
>
> I would like to implement a macro which sets a field in a struct only
> if a certain preprocessor symbol has been defined.
>
[...]
The following works for me in C99 mode:
__________________________________________________ _____________
#include <stdio.h>
#include <stdlib.h>
typedef struct thing {
int bar;
int foo;
} Thing;
#ifndef CONFIG_NOFOO
#define REGISTER_FOO(foo_) .foo = foo_
#else
#define REGISTER_FOO(foo_)
#endif
Thing thing1 = {
.bar = 1,
REGISTER_FOO(42)
};
void thing_print(Thing *t)
{
printf("bar: %d\n", t->bar);
printf("foo: %d\n", t->foo);
}
int main(void)
{
thing_print(&thing1);
getchar();
return 0;
}
__________________________________________________ _____________
|