문제

C ++ 클래스를 사용하여 사용자가 구성한 프로그램이 있습니다. 동일한 클래스를 사용하여 프로그램을 구성해야합니다. C99 (CL 언어 열기)의 하위 집합 만 사용할 수 있습니다.

이 질문은 다음과 같습니다. C ++에서 C-Code에 컴파일하는 방법이 있습니까?

오픈 소스는 훌륭합니다!

도움이 되었습니까?

해결책

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.

다른 팁

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

  1. Comeau Computing offers a compiler based on Edison Design Group's front end that outputs C code.
  2. 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.
  3. 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).
  4. 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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top