tags:

views:

2054

answers:

4

Hello,

I have a Code which is C++ (*.cpp - source, *.hpp - header files).

In the overall code,there are many classes defined, their member functions, constructors, destructors for those classes, few template classes and lots of C++ stuff. Now i need to convert the source to plain C code. I have following questions -

1.) Is there any tool to convert C++ code and header files to C code?

2.) Or will I have to do total rewrite of the code(i will have to remove the constructors,destructors and move that code into some init(),deinit() functions; change classes to structures, make existing member functions as function pointers in those newly defined structures and then invoke those functions using function pointes etc..)?

3.) If at all i have to convert manually myself, what all C++ specific code-data constructs/semantics i need to take care while doing the conversion to C specific code-data semantics?

Thank you.

-AD

+2  A: 

The obvious question is, why do you think that you need to convert it? This will not be a simple task, believe me.

anon
+8  A: 

There is indeed such a tool, Comeau's C++ compiler. . It will generate C code which you can't manually maintain, but that's no problem. You'll maintain the C++ code, and just convert to C on the fly.

MSalters
@MSalters: Thanks for the pointer about Comeaus compiler. But sadly that doesnt serve my purpose, as comeaus compiler intermediate format c code is not possible to obtain, and even if we obtain somehow its not compilable by normal ANSI-C compilers.
goldenmean
If your code does not use exceptions and templates you might have a chance to get an old copy of cfront to work on your code. But as MSalters said it's going to be ugly :-)
lothar
it would also have to use no standard library functionality
anon
+3  A: 

While you can do OO in C (e.g. by adding a theType *this first parameter to methods, and manually handling something like vtables for polymorphism) this is never particularly satisfactory as a design, and will look ugly (even with some pre-processor hacks).

I would suggest at least looking at a re-design to compare how this would work out.

Overall a lot depends on the answer to the key question: if you have working C++ code, why do you want C instead?

Richard
A: 

http://llvm.org/docs/FAQ.html#translatecxx

PS: i haven't used it at all. let me know if it works.

plan9assembler
@plan9assembler: Thanks for the pointer. I will check it out and let u know.
goldenmean