4 ms·
IMO, any discussion about C metaprogramming should mention m4 [0], [1], a superior alternative to preprocessor macro system. [0] http://www.gnu.org/software/m4
by fizixer 11y ago
IMO, any discussion about C metaprogramming should mention m4 [0], [1], a superior alternative to preprocessor macro system.
[0] http://www.gnu.org/software/m4/m4.html http://www.gnu.org/software/m4/m4.html
[1] http://www.ibm.com/developerworks/library/l-metaprog1/ http://www.ibm.com/developerworks/library/l-metaprog1/
- comex 11y agoIt's easy to be better than cpp, but without the ability to inspect the AST rather than raw text, you can't do the really cool things that macros in languages from Scheme to Rust can. (I've also heard bad things about m4's usability, though I haven't learned to use it myself.) If anyone wants to try to retrofit such functionality into C programs and is willing to add an extra command to the build (as m4 would require), I'd suggest implementing the macros in Python and using pycparser - or using some language's clang bindings or other compiler interfaces, but that has the disadvantage of more dependencies.
- fizixer 11y agoYou raise a very interesting point. In fact, python and C being my favorite two languages, and I'm trying to do some metaprogramming in some python-C hybrid way, I'd be very interested in looking into this. The other thing I'm spending a lot of time on these days is 'TeX' (specifically LaTeX but I've explored the internals of the system a bit). As TeX is also a macro system, which probably could be made to work as a general-purpose macro system, I'm interested in how cpp, m4, TeX, and lisp/scheme macros compare and contrast.
- mzs 11y agoI've had some success doing the meta programming in python, which generates .cpp (since the preprocessor does some expansion, it's not C++) files that I include from and .h file which I include in C code. It's harder to go the other way. It lets you basically modify only the python source and then have the same names for structs (classes in python) in both languages. But then I realized it was all pretty complicated. So I started using ctypes instead. That way I only wrote it once in a header file. The only real limitation when using cpython was that I could only include a maximum of one other struct in each struct, so sometimes the C header files were a bit strangely written.