Is it possible to convert C++ to C? If you mean "can you convert C++ to readable and maintainable C", that's probably not possible except for some limited subsets of C++. In general, there's no direct mapping of C++ features into C, and the generated code would have to be quite convoluted. If you mean "can you convert C++ to C, and run the C through a C compiler to get object code", as a way to run C++ code on a system that has only a C compiler, yes, that can be done. EDG's C++ Front End can be configured to do that, and Comeau Computing sells end-user versions of that. It is possible to implement all of the features of ISO standard C++ by translation to C, and except for exception handling this produces object code with efficiency comparable to that of the code generated by a conventional compiler. For exception handling, it is possible to do an implementation using Even if you manage to translate C++ to C, your problems may not be over. If you try to mix code generated through this process with object code or libraries generated by another C++ compiler, you are likely to find (as happens whenever you mix two C++ compilers) that the two varieties of object code are incompatible for various reasons (e.g., layout of classes, name mangling). If you mix the C code with native C code, you may be okay. However, the generated code must be linked with a runtime, which must be configured for the target environment, and the generated code itself varies somewhat depending on the target environment (e.g., on the size of basic data types). So it's not the case that you can translate C++ to C for one system and then be confident that the generated C code will be portable to any other system. Our front end needs to be ported to the host and target environment. Next, we come to the issue of libraries: If your program makes use of C library functions (such as Even if you get your C++ code linked and running on the target system, you have one final hurdle to leap: debugging. Your debugger will be getting its debug information from the C compiler, and that means it doesn't get a lot of the information present in the C++ source code. You'll have to debug mostly at the C level. And one more issue: language extensions. EDG's C++ Front End accepts a large number of Microsoft and GNU extensions, depending on the compilation mode selected, and while some of those features can be converted to standard C others cannot. For example, the GNU Can I use your JFE to translate my Java program to C? Maybe. This involves some of the same issues mentioned in the previous question. More information is available here. |










