Frequently Asked Questions
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
longjmp that is completely conformant, but the code generated will be 5-20%
slower than code generated by a true compiler.
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
cos), you're probably okay. You may have to make a version of the header files that wraps
the function declarations inside an
extern "C" block to be able to call the C
functions from C++, but otherwise it should work. If your program uses C++ libraries, however, whether
standard ones such as iostreams or third-party products like GUI libraries, you still have problems. If you
have the libraries in object code form, they're probably not compatible unless they were compiled by Cfront
or a compiler that uses the IA-64 ABI. If you have the libraries in source code form, you can compile them
to object code yourself, but you may still find that the library uses some nonstandard tricks that depend on
a particular object layout. Do note that EDG's product does not include any standard libraries (e.g., for
iostreams); if you need libraries, you can get them from companies like Dinkumware,
or you can use the GNU libraries.
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
__attribute__((__weak__)) indicates that an
external name should be passed to the linker as a "weak" external, which may have multiple
definitions. There's no way to request that in standard C. Those kinds of extensions are passed through to
the generated C, and will work only if your target C compiler understands them.
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.