How to convert C ++ code to C

I have C ++ code. The code defines a lot of classes, their member functions, constructors, destructors for these classes, several classes of templates, and a lot of C ++ materials. Now I need to convert the source code to simple code.

I have the following questions:

  • Is there any tool to convert C ++ code and header files to C code?

  • Should I do a complete rewrite of the code (I will have to remove the constructors, destructors and move this code to some init() , deinit() functions, change the classes to structures, make the existing member functions as function pointers in these newly defined structures, and then call these functions using function pointers, etc.)?

  • If I need to convert it myself, what C ++ specific constructors / semantics of code data do I need to pay attention to when converting from C ++ to C?

+41
c ++ c code-translation
Apr 10 '09 at 10:30
source share
6 answers

There is such a tool, the Comeau C ++ compiler. It will generate C code that you cannot save manually, but that is not a problem. You will support C ++ code and just convert to C on the fly.

+29
Apr 10 '09 at 10:37
source share

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

PS: I have not used it at all. let me know if that works.

+17
Apr 10 '09 at 10:48
source share

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

I would suggest at least looking at redesigning to compare how this will work.

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

+6
Apr 10 '09 at 10:38
source share

Maybe a good ol ' cfront will do?

+5
Oct 03 '12 at 23:05
source share

The compiler consists of two main blocks: "front end" and "back end". The front end of the compiler analyzes the source code and creates some form of “intermediate representation” of the mentioned source code, which is much easier to parse by a machine algorithm than the source code (that is, while the source code, for example, C ++, is designed to help the programmer a person to write code, the intermediate form is intended to simplify the algorithm, which more easily analyzes the specified intermediate form) The back end of the compiler takes an intermediate form and then converts it to the "target language".

Now the target language for general purpose compilers is assembler languages ​​for different processors, but there is nothing that could prevent the compiler from completing work on code in some other language until the specified target language is (at least) as a common processor collector .

Now, as you can imagine, C is definitely as flexible as the CPU assembler, so the C ++ to C compiler is really not a problem to implement from a technical point of view.

So you have: C ++ --- frontEnd ---> someIntermediaryForm --- backEnd ---> C

You can check out these guys: http://www.edg.com/index.php?location=c_frontend (the link above is just informative for what you can do, they license their front ends for tens of thousands of dollars).

PS As far as I know, there is no such C ++ compiler for C GNU, and it completely beats me (if I'm right about this). Since the C-language is quite small and the internal mechanisms are rather rudimentary, the C compiler requires something like one man-year of work (I can tell you that I wrote this compiler myself a few years ago and it creates [virtual] stack intermediate code) and the ability to have a supported, updated C ++ compiler, although only writing a C compiler would one day be wonderful ...

+4
Jan 10 '13 at 16:35
source share

This is an old thread, but C++ Faq seems to have a section. This, apparently, will be updated if contacted with the author, so it is likely to be more relevant in the long term.

+2
Mar 12 '13 at 18:37
source share



All Articles