Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ to C conversion workflow [closed]

Tags:

c++

c

First thing, I am not looking for a tool that is going to produce an unmaintainable C code from an existing cpp code-base. It is a requirement from a client, so I cannot just use the CPP code base itself.

I am trying to work out a work-flow, so that I can convert the code-base from CPP to C incrementally without breaking the code. I have thought out a few things using the "extern C" approach

  1. Classes to Structs
  2. Member functions of the Classes will be converted to Struct_name_FunctionName format.
  3. In case of a function being re-used by 2 classes, I plan to use function pointers.
  4. Replace overloaded operators like +,- etc with actual functions. Eg a Add_, Sub_ etc

Can you folks add anymore ? and point out potential pit-falls ? I cannot share the code-base because of NDA. The code base isn't huge in itself. Its got around 50 cpp files, saw some 100 odd classes.

Thanks in advance.

P.s I have gone through some other questions that sound similar. But they don't really have the work-flow that I am looking for.

[Closing] : Although the post has been put on hold because it is too broad, many of the comments were in fact useful. I am clearer on what needs to be done than before. Thank you.

like image 342
vikasmk Avatar asked Feb 06 '15 07:02

vikasmk


1 Answers

Well, there's a very long list you'll have to tackle. Without claiming to be complete, you also need:

  1. Virtual function support (not impossibly hard, function pointer table)
  2. Constructors and destructors (Reasonable mapping to ordinary functions, lot of work)
  3. Exceptions and stack unwinding (extremely hard)
  4. Missing C++ library functions and classes (lot of work)

But don't kid yourself. std::map<std::string, std::pair<int, int>> is a very straightforward class that stores two ints for each string. The C translation of that class is an outright mess. MyMap["five,three"] = std::make_pair(5,3) can easily become 100+ lines of C code.

like image 105
MSalters Avatar answered Oct 17 '22 03:10

MSalters