|Syntactic Confectionery Delight|
[OT] Abusing the C preprocessorby afoken (Abbot)
|on Nov 10, 2017 at 17:50 UTC||Need Help??|
afoken has asked for the
wisdom of the Perl Monks concerning the following question:
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:
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:
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):
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.
Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)