Re: Anyone know of a C preprocessor #define introspection mechanism? (for FFI bindings)

  • From: Steve Atkins <steve@xxxxxxxxxxx>
  • To: luajit@xxxxxxxxxxxxx
  • Date: Tue, 10 Feb 2015 18:18:43 -0800

On Feb 10, 2015, at 6:10 PM, demetri <demetri.spanos@xxxxxxxxx> wrote:

> Sorry, I know this is an odd question, but having a positive answer 
> would greatly simplify some of our automated FFI code generation.
> I would like some way to detect at compile time that a preprocessor 
> constant, say, AF_INET, is defined and automatically emit a line like
> int lj_ffi_cpp_constant_AF_INET = AF_INET;
> so that I could then, in FFI code, do something like:
> whatever = ffi.C.lj_ffi_cpp_constant_AF_INET
> I realize this is a bit of a stretch but if anyone knows of a way to
> get at the #define state of the C preprocessor so that such a thing
> could be done, I would greatly appreciate it :)
> In moments of desperation I have considered an automated brute
> force strategy enumerating all possible constant names of a given
> size, emitting a test program to printf the value, testing for successful
> compilation, and then adding that to a database of known values. Even
> if this took a dat to run it might be worthwhile in terms of saving 
> developer  heartache, given our need to build on multiple platforms with
> multiple bitness with multiple versions of dependencies.
> Please don't hesitate to propose even awkward solutions! Nothing
> could be worse than the brute-force plan. And even an awkward solution
> is probably better than the manual #define constant curation that we
> currently do.

"gcc -dM -E file.c" will write to stdout the current set of preprocessor
symbols, after preprocessing file.c and anything it #includes. The same
will work for clang, icc and (I think) the HP and portland group compiler.

Sun Studio (or whatever it's called, if it still exists) is "-xdumpmacros -E".

I've no idea if there's anything equivalent for visual studio.


Other related posts: