Discussion Locked

Convert from C++ into C plain

0 helpful
9 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

I have a very big C++ program. But now i have to convert it into C plain. Please tell me how I can do ? And about constructor and deconstructor, how can I rewrite them ?
Thanks for your attention.

9 replies

by mightyduk

blood, sweat, and tears... Maybe this is a dumb question... why?

Can you use Object C? That may be an easier job.

Constructors/Destructors have no C equivalents, they are part of the object oriented features of C++, which you would have to rewrite extensively to make them work.

by dachev

No easy way.
C++ is a serious upgrade to the C language and many of it's abilities are not present in C. Constructors/Destructors are one of these upgrades.
You need to do everything by your self - manually calling constructors, manually take care about keeping the functions and data from one class together (eventually by arranging their names, or keeping them in different files), etc...

This will wriley be a lot of work. Sorry.

by wayne m.

If at all possible, I'd recommend finding a C++ compiler for your target machine. If that is not possible, here are some ideas off the top of my head:

Change all classes to structs

Rename all methods from Class::method to Class_Method. Constructors could become Class_Contstructor and Destructors could become Class_Destructor.

Separate overloaded methods with some extension.

Explicitly call the constructors and destructors when classes go out of scope.

Virtual functions will require a pointer to a jump table in the base class. Constructors will have to update this pointer as they are called. The base class virtual functions will have to call through this table.

Exception throws will have to be replaced with a call to the abort() function. There is no easy way to implement Exception Catch blocks.

A good reference showing how to convert between C and C++ is "Class Construction In C And C++" by Roger Sessions.

Good luck, this may be a difficult undertaking.

by jpereira

You don't say what your platform is. If it is Unix or Linux, then go to the GNU home page (www.gnu.org) and download the GNU compiler which has C++ support. If it's Windows, go to your local software store and buy Visual C++. You can justify the cost by pointing out that it is going to take days or weeks of your time to rewrite the code. Because, that's what it is - a rewrite.

This discussion is closed to new replies.