Converting C source to C++ Converting C source to C++ c c

Converting C source to C++


Having just started on pretty much the same thing a few months ago (on a ten-year-old commercial project, originally written with the "C++ is nothing but C with smart structs" philosophy), I would suggest using the same strategy you'd use to eat an elephant: take it one bite at a time. :-)

As much as possible, split it up into stages that can be done with minimal effects on other parts. Building a facade system, as Federico Ramponi suggested, is a good start -- once everything has a C++ facade and is communicating through it, you can change the internals of the modules with fair certainty that they can't affect anything outside them.

We already had a partial C++ interface system in place (due to previous smaller refactoring efforts), so this approach wasn't difficult in our case. Once we had everything communicating as C++ objects (which took a few weeks, working on a completely separate source-code branch and integrating all changes to the main branch as they were approved), it was very seldom that we couldn't compile a totally working version before we left for the day.

The change-over isn't complete yet -- we've paused twice for interim releases (we aim for a point-release every few weeks), but it's well on the way, and no customer has complained about any problems. Our QA people have only found one problem that I recall, too. :-)


What about:

  1. Compiling everything in C++'s C subset and get that working, and
  2. Implementing a set of facades leaving the C code unaltered?

Why is "translation into C++ mandatory"? You can wrap the C code without the pain of converting it into huge classes and so on.


Your application has lots of folks working on it, and a need to not-be-broken.If you are serious about large scale conversion to an OO style, whatyou need is massive transformation tools to automate the work.

The basic idea is to designate groups of data as classes, and thenget the tool to refactor the code to move that data into classes,move functions on just that data into those classes,and revise all accesses to that data to calls on the classes.

You can do an automated preanalysis to form statistic clusters to get some ideas,but you'll still need an applicaiton aware engineer to decide whatdata elements should be grouped.

A tool that is capable of doing this task is our DMS Software ReengineeringToolkit. DMS has strong C parsers for reading your code, captures the C codeas compiler abstract syntax trees, (and unlike a conventional compiler)can compute flow analyses across your entire 300K SLOC.DMS has a C++ front end that can be used as the "back" end;one writes transformations that map C syntax to C++ syntax.

A major C++ reengineering task on a large avionics system givessome idea of what using DMS for this kind of activity is like.See technical papers atwww.semdesigns.com/Products/DMS/DMSToolkit.html,specificallyRe-engineering C++ Component Models Via Automatic Program Transformation

This process is not for the faint of heart. But than anybodythat would consider manual refactoring of a large applicationis already not afraid of hard work.

Yes, I'm associated with the company, being its chief architect.