Well, you cannot use #if conditions in a macro replacement. You could use indirection, though:

#define CFG_USED(type, name, stuff) \ { .compFunc = compare_ ## type; /* */ }, #define CFG_UNUSED(type, name, stuff) /* unused */ #define CFG_ADD(type, name, stuff) CFG_USE_ ## type (type,name,stuff) #ifdef USING_int #define CFG_USE_int CFG_USED #else #define CFG_USE_int CFG_UNUSED #endif ...

Regarding warnings: you can suppress unused warnings on individual basis with an __attribute__((__unused__)).

The library approach suggested by roboticus is a good one. Let the linker work it out where possible. However, some types may not be available on all platforms, etc., so this is not always an option.

But then, there are more options. You could define the compare functions via inline versions:

static inline int _inline_cmpfunc_int(void *a, void *b) { int _a = *(int*)a, _b = *(int*)b; return (_a > _b) - (_a < _b); }
There should be no warnings for unused inline functions. After that, you might use the same configure mechanism to conditionally generate function definitions:
static foo compare_foo(void *a, void *b) { return _inline_cmpfunc_foo(a, b); }
Tricks like #include "definitions.h" multiple times, are sometimes used.


In reply to Re: [OT] Abusing the C preprocessor by Anonymous Monk
in thread [OT] Abusing the C preprocessor by afoken

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.