Is it possible to convert C++ to C?

old link: c++-faq/convert-to-c

new link: isocpp.org

答案是:

  • NO. 不能把C++代码自动转换成可读性和可维护性的C代码
  • YES. 能自动转换,但有限制(exception handling 无法转换)

Depends on what you mean. If you mean, Is it possible to convert C++ to readable and maintainable C-code? then sorry, the answer is No — C++ features don’t directly map to C, plus the generated C code is not intended for humans to follow. If instead you mean, Are there compilers which convert C++ to C for the purpose of compiling onto a platform that yet doesn’t have a C++ compiler? then you’re in luck — keep reading.

A compiler which compiles C++ to C does full syntax and semantic checking on the program, and just happens to use C code as a way of generating object code. Such a compiler is not merely some kind of fancy macro processor. (And please don’t email me claiming these are preprocessors — they are not — they are full compilers.) It is possible to implement all of the features of ISO Standard C++ by translation to C, and except for exception handling, it typically results in object code with efficiency comparable to that of the code generated by a conventional C++ compiler.

Here are some products that perform compilation to C (note: if you know of any other products that do this, please let us know):

  • 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.
  • 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).

Note that you typically need to specify the target platform’s CPU, OS and C compiler so that the generated C code will be specifically targeted for this platform. This means: (a) you probably can’t take the C code generated for platform X and compile it on platform Y; and (b) it’ll be difficult to do the translation yourself — it’ll probably be a lot cheaper/safer with one of these tools.

One more time: do not email me saying these are just preprocessors — they are not — they are compilers.

llvm emit 功能

在 google 怎么用 llvm 转换C++源代码到C源代码时,发现了这个 http://llvm.org/releases/3.1/docs/ReleaseNotes.html#changes LLVM 从3.1版本就不再支持从其它语言转换成C源代码的功能 但转换成汇编的功能继续可用

其它参考: https://en.wikipedia.org/wiki/Source-to-source_compiler

Clang emit C 代码的测试

Compile your program with llvm-g++:

1
% llvm-g++ -emit-llvm x.cpp -o program.bc -c

or:

1
2
3
% llvm-g++ a.cpp -c -emit-llvm
% llvm-g++ b.cpp -c -emit-llvm
% llvm-ld a.o b.o -o program

This will generate program and program.bc. The .bc file is the LLVM version of the program all linked together.

Convert the LLVM code to C code, using the LLC tool with the C backend:

1
% llc -march=c program.bc -o program.c

Finally, compile the C file:

1
% cc x.c -lstdc++