c - Generate #ifdef, #endif clause with another macro -
i'm having problem in program i'm working on. i'm trying in --help display features compiled in or not. however, there quite alot of these , "normal" way verbose. e.g.:
#ifdef have_foo static const bool have_foo = true; #else static const bool have_foo = false; #endif printf("support foo: %s\n", have_foo ? "yes" : "no");
now, since have every feature, loads of lines, not want.
so thought i'd write macros it:
#define _supp(x) #ifdef have_##x \ static const bool _##x##_supp = true; \ #else \ static const bool _##x##_supp = false; \ #endif #define _printsupp(var, name, desc) printf("\t%s - %s: %s\n", name, desc, _##var##_supp ? "yes" : "no")
however, there issue here. macro expanded single line, , preprocessor chokes on this. there way generate macro actual newlines inbetween, or possible evaluate #ifdef
on single line?
if, instead of not defining have_foo
, define 0
, can do:
const struct { bool present; const char *name; } features[nfeatures] = { {have_foo, "foo"}, {have_bar, "bar"}, ... }; (size_t i=0; < nfeatures; i++) if (features[i].present) printf(" ... , we've got: %s\n", features[i].name);
you'll have check #if have_foo
instead of #ifdef have_foo
then, , --help
message may displayed bit slower if number of features runs in zillions (in case i'd recommend different architecture anyway).
Comments
Post a Comment