Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question 开发者_如何学运维I have a program which is configured by the user by using C++ classes and the same class should be used to configure a program which can only use a subset of C99 (Open CL Language).
So my question is: Is there a way to compile C++ to C-Code?
Open Source would be great!
You could use the clang C++ frontend to generate llvm bytecode, and use llc
to emit C code, see llc doc, especially the c
option. Both are open source, with BSD like licenses.
The C++ FAQ has a list of possibilities: Is it possible to convert C++ to C?.
In short, it says that you can't expect this to give you particularly readable code. Think of the complexities involved; multiple inheritance, virtual-function resolution, templates, operator overloading, etc., etc. There's no clean succinct way of expressing these concepts in pure C. If all you're after is compilable C, though, then this is probably the way to go.
The Comeau compiler seems to be able to do that. From Wikipedia "Rather than produce an executable directly, Comeau C/C++ outputs C code and requires a separate C compiler in order to produce the final program."
I have never tried it, though.
VisualGBD is a Visual Studio extension that allows you to develop in compile for embedded platforms. It's not free but there is a trial. Worth it in my mind if it works for your application.
https://visualgdb.com
LLVM is a good option.
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.cp -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.
The Comeau site is dead unfortunately. Coherent is built on comeau and might offer some options. They have prebuilt VMs which have been maintained as recent as 2018.
https://www.autometer.de/unix4fun/coherent/#inst_coh
CLang is available but has not been updated in a long time
http://www.softwarepreservation.org/projects/c_plus_plus/cfront/release_3.0.3/source/cfront_3_0_3.tgz
- Comeau Computing offers a compiler based on Edison Design Group's front end that outputs C code.
- LLVM is a downloadable compiler that emits C code. See also here and here. Here is an example of C++ to C conversion via LLVM.
- Cfront, the original implementation of C++, done by Bjarne Stroustrup and others at AT&T, generates C code. However it has two problems: it's been difficult to obtain a license since the mid 90s when it started going through a maze of ownership changes, and development ceased at that same time and so it doesn't get bug fixes and doesn't support any of the newer language features (e.g., exceptions, namespaces, RTTI, member templates).
- Contrary to popular myth, as of this writing there is no version of g++ that translates C++ to C. Such a thing seems to be doable, but I am not aware that anyone has actually done it (yet).
http://www.cs.technion.ac.il/users/yechiel/c++-faq/convert-to-c.html
精彩评论