22

Any suggestion for template base code generator for C / C++ specifically to generate repetitive code generation? (Not UML / MATLAB model based or other advanced stuff). For a newbie in this field any good generic tutorial (not tool based)?

I came across GNU Autogen looks good but looks like it needs a steep learning curve. I would prefer some plug-in for eclipse like IDE, easy to use and most importantly good tutorials.

Kamath
  • 4,461
  • 5
  • 33
  • 60

5 Answers5

17

The basic concept of code generation is simple enough - and people's needs are varied enough - that there are quite a few options out there.

  • Boost.Preprocessor is a library of functions built on top of the standard C / C++ preprocessor that makes it much easier to use the preprocessor to do code generation. It's not as flexible as other options, and figuring out preprocessor errors can be tricky, but the fact that it uses only standard language features greatly simplifies using it and integrating it into your builds.
  • If you know Python, there's Cog.
  • Some of Google's projects use Pump.
  • There are many general-purpose templating solutions (Python's Genshi, eRuby, etc.). These are often designed for generating HTML and XML but also work for code.
  • It's also easy enough to hack something together in the scripting language of your choice.

Without knowing more about what your needs are and what tools you're comfortable with, I can't give a more specific recommendation.

I'm not familiar with anything that provides an Eclipse plugin.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Josh Kelley
  • 56,064
  • 19
  • 146
  • 246
4

If you know Python, then Cog could be considered as light-weight solution: http://www.python.org/about/success/cog/

Rost
  • 8,779
  • 28
  • 50
  • 1
    tried cog. it rocks. easy, clean, maintainable. I started using for even basic tasks like generating my header file declarations automatically from my .cpp files :-) – Hugh Perkins Dec 07 '14 at 08:22
1

Look at my answer for a similar question for Java classes using M2T-JET, an eclipse based, lightweight templating generator. JET is language agnostic and you can see from the example that it's fairly easy to use.

Community
  • 1
  • 1
Chris Gerken
  • 16,221
  • 6
  • 44
  • 59
1

I appreciate using Lua for this task, with something like Templet or one of another myriad of Lua-based preprocessors. The benefit of using Lua over something like Python is that you can, if necessary, include the source code to your template processor and a basic Lua installation along with whatever it is you are shipping. You may then add the compilation of Lua and subsequent template files to the build process as usual.

I would advise not using Python-based solutions for one reason: juggling various pythons to satisfy every developer's use of a completely different yet incompatible version is annoying. If you choose to use a language which you can't embed in your trees, you'll want to make sure pre-computed versions are available.

Skrylar
  • 762
  • 6
  • 16
1

Late to the party but I would recommend Codeworker Its the only tool I found that does everything the above tools do and more. It has the Python Cog like functionality for embedded generation, it has the template based generation like Templet or Pump. And it has the rather useful feature of protected areas so you can customise your code as needed and re-generate.

I have used it for generating all the boiler plate c++ code as well as configuration for projects like SQL, config, javascript etc.

Delta_Fore
  • 3,079
  • 4
  • 26
  • 46