Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert C++ Code to C [closed]

I have some C++ code. In the 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 the have following questions:

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

  2. 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 pointers etc..)?

  3. If I have to convert it manually myself, what C++ specific code-data constructs/semantics do I need to pay attention to while doing the conversion from C++ to C?

like image 712
goldenmean Avatar asked Apr 10 '09 10:04

goldenmean


People also ask

Can you compile C++ to C?

Any C compiler that is compatible with the Oracle Developer Studio C compiler is also compatible with the Oracle Developer Studio C++ compiler. The C runtime library used by your C compiler must also be compatible with the C++ compiler.

How do I convert a C++ code to C Is there an online converter?

It is technically impossible to convert C++ code to C code because both languages although seem similar work very differently at binary level. Also, C does not support any aspect of object oriented programming and thus have no “C++ equivalent”.

Can a .C file be C++?

Incompatibilities are few and far between. You're going to have to "fix" the C files to be C++ compatible if you want to use them in a C++ compiler or you can link the C object files separately. The only problem I've encountered personally is C's implicit conversion from void* to any other pointer type. – Ed S.


2 Answers

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.

like image 181
MSalters Avatar answered Oct 06 '22 12:10

MSalters


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

It handles some code, but will fail for more complex implementations as it hasn't been fully updated for some of the modern C++ conventions. So try compiling your code frequently until you get a feel for what's allowed.

Usage sytax from the command line is as follows for version 9.0.1:

clang -c CPPtoC.cpp -o CPPtoC.bc -emit-llvm clang -march=c  CPPtoC.bc -o CPPtoC.c 

For older versions (unsure of transition version), use the following syntax:

llvm-g++ -c CPPtoC.cpp -o CPPtoC.bc -emit-llvm llc -march=c  CPPtoC.bc -o CPPtoC.c 

Note that it creates a GNU flavor of C and not true ANSI C. You will want to test that this is useful for you before you invest too heavily in your code. For example, some embedded systems only accept ANSI C.

Also note that it generates functional but fairly unreadable code. I recommend commenting and maintain your C++ code and not worrying about the final C code.

EDIT : although official support of this functionality was removed, but users can still use this unofficial support from Julia language devs, to achieve mentioned above functionality.

like image 30
plan9assembler Avatar answered Oct 06 '22 11:10

plan9assembler