Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to compile C++ to C Code? [closed]

Tags:

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!

like image 780
cl_progger Avatar asked Feb 19 '11 10:02

cl_progger


People also ask

Can you run C without compiling?

If you only have a ". c" file, then you don't have a program. You must compile the program first to make an executable, and then you must run the program to get its output.

Can you compile C++ to C?

If you declare a C++ function to have C linkage, it can be called from a function compiled by the C compiler. A function declared to have C linkage can use all the features of C++, but its parameters and return type must be accessible from C if you want to call it from C code.

Can I use g ++ to compile C code?

g++ also has additional macros. So you can compile C code with g++ and in fact mix both C and C++ code. Finally, the executable size may also changes based on the compiler.

How are C codes compiled?

Compilation process in C involves four steps: pre-processing, compiling, assembling, and linking. The preprocessor tool helps in comments removal, macros expansion, file inclusion, and conditional compilation. These commands are executed in the first step of the compilation process.


2 Answers

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.

like image 189
tonio Avatar answered Oct 06 '22 00:10

tonio


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.

like image 39
Oliver Charlesworth Avatar answered Oct 05 '22 23:10

Oliver Charlesworth