Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Hi!

At work, I'm currently using x-macros to allow compile-time configuration of some filtering software. It boils down to a configuration file that roughly looks like this:

CFG_START() CFG_ADD(int, FOO, /* some more stuff */) CFG_ADD(short, BAR, /* some more stuff */) CFG_ADD(float, BAZ, /* some more stuff */) CFG_END()

This file generates some variables, and a meta-data table for the running software so it can know the types (int, short, float) for the variables associated with the identifiers (FOO, BAR, BAZ).

So far, no problems, just a little bit of creative usage of the C preprocessor.

The running software has to invoke qsort. qsort needs a compare function. The meta-data table contains pointers to the compare function matching the type, simply by appending the type name to a common prefix:

/* simplified */ MetaData_t metadata = { #define CFG_ADD(TYPE,NAME,STUFF) { .compFunc = compare_ ## TYPE; / +* and some more stuff */ } #include "configfile.h" #undef CFG_ADD }

My source contains a lot of compare functions for different types that could be used in the configuration file. But not all of the functions are actually used, because not all of the types are used in the configuration file. So, the compiler warns about some unused functions (e.g. compare_double() for the example shown above).

Yes, it is harmless, but ugly. Our code should compile without warnings. Disabling the warning is not an option. I would prefer to be able to hide unused functions from the C compiler.

Something like this (and yes, I know this won't work):

#define CFG_ADD(TYPE,NAME,STUFF) \ #ifndef USING_ ## TYPE /* does not work */ \ #define USING_ ## TYPE /* does not work */ \ #endif #include "configfile.h" #undef CFG_ADD /* and later: */ #ifdef USING_int static int compare_int(const void * a, const void * b); #endif #ifdef USING_double static int compare_double(const void * a, const void * b); #endif /* further down */ #ifdef USING_int static int compare_int(const void * a, const void * b) { /* ... */ } #endif #ifdef USING_double static int compare_double(const void * a, const void * b) { /* ... */ } #endif

Any clever ideas that I missed?

Environment: GCC 4.x cross-compiling for an embedded system (so code and data size might become a problem). GCC might be updated in the future, but switching to a different compiler is very unlikely. So a GCC-only solution is acceptable.

Alexander

--
Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

In reply to [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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (6)
As of 2024-04-19 06:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found