Generating code at compile-time using scripts

You do most likely need to tweak the Makefile a bit. It would be easy to write a (Python) script that reads each of your source files as an additional preprocessing step, replacing instances of generate_boring_functions (or any other script-macro) with the correct code, potentially just by invoking generate_boring_functions.py with the right arguments, and bypassing the need for temporary files by sending the source to the compiler over standard input.

Damn, now I want to make something like this.

Edit: A rule like this, stuck in a makefile, could be used to handle the extra build step. This is untested and added only for some shot at completeness.

%.o : %.cpp
    python macros.py $< | g++ -x cpp -c - -o $@

If a makefile isn't conventional enough for you, you could get by with cleverly-written macros.

class FooBarClass
{
    DEFINE_BORING_METHODS( FooBarClass )

    /* interesting functions begin here */
}

I very frequently see this done to implement the boilerplate parts of COM classes.

But if you want something that's neither make nor macro, then I don't know what you could possibly mean.


A makefile (or equivalent) is a "conventional" means!

Tags:

C++

C

Gcc