如何将 C++ 代码转换为 C
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/737257/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to convert C++ Code to C
提问by goldenmean
I have some C++ code. In the code there are many classes defined, their member functions, constructors, destructors for those classes, few template classes and lots of C++ stuff. Now I need to convert the source to plain C code.
我有一些 C++ 代码。在代码中定义了许多类,它们的成员函数、构造函数、这些类的析构函数、很少的模板类和大量的 C++ 东西。现在我需要将源代码转换为纯 C 代码。
I the have following questions:
我有以下问题:
Is there any tool to convert C++ code and header files to C code?
Will I have to do total rewrite of the code (I will have to remove the constructors,destructors and move that code into some
init()
,deinit()
functions; change classes to structures, make existing member functions as function pointers in those newly defined structures and then invoke those functions using function pointers etc..)?If I have to convert it manually myself, what C++ specific code-data constructs/semantics do I need to pay attention to while doing the conversion from C++ to C?
是否有任何工具可以将 C++ 代码和头文件转换为 C 代码?
我必须做的代码完全重写(我将不得不删除构造函数,析构函数和代码搬进了
init()
,deinit()
功能;改变类的结构,使现有的成员函数中的新定义的结构函数指针,然后调用这些函数使用函数指针等......)?如果我必须自己手动转换它,在进行从 C++ 到 C 的转换时,我需要注意哪些 C++ 特定的代码数据结构/语义?
回答by MSalters
There is indeed such a tool, Comeau's C++ compiler. . It will generate C code which you can't manually maintain, but that's no problem. You'll maintain the C++ code, and just convert to C on the fly.
确实有这样的工具,Comeau 的 C++ 编译器。. 它将生成您无法手动维护的 C 代码,但这没问题。您将维护 C++ 代码,并即时转换为 C。
回答by plan9assembler
http://llvm.org/docs/FAQ.html#translatecxx
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.
它处理一些代码,但对于更复杂的实现将失败,因为它尚未针对某些现代 C++ 约定进行完全更新。因此,请尝试经常编译您的代码,直到您了解允许的内容为止。
Usage sytax from the command line is as follows for version 9.0.1:
对于 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.
请注意,它创建了 C 的 GNU 风格,而不是真正的 ANSI C。在对代码进行过多投资之前,您需要测试它是否对您有用。例如,一些嵌入式系统只接受 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.
另请注意,它会生成功能强大但相当不可读的代码。我建议注释和维护您的 C++ 代码,而不用担心最终的 C 代码。
回答by Richard
While you can do OO in C (e.g. by adding a theType *this
first parameter to methods, and manually handling something like vtables for polymorphism) this is never particularly satisfactory as a design, and will look ugly (even with some pre-processor hacks).
虽然您可以在 C 中执行 OO(例如,通过向theType *this
方法添加第一个参数,并手动处理诸如 vtables 之类的多态性),但作为设计,这从来都不是特别令人满意,并且看起来很丑陋(即使有一些预处理器黑客)。
I would suggest at least looking at a re-design to compare how this would work out.
我建议至少考虑重新设计以比较这将如何解决。
Overall a lot depends on the answer to the key question: if you have working C++ code, why do you want C instead?
总的来说,很大程度上取决于关键问题的答案:如果您有可用的 C++ 代码,为什么要使用 C 来代替?
回答by Gyll
A compiler consists of two major blocks: the 'front end' and the 'back end'. The front end of a compiler analyzes the source code and builds some form of a 'intermediary representation' of said source code which is much easier to analyze by a machine algorithm than is the source code (i.e. whereas the source code e.g. C++ is designed to help the human programmerto write code, the intermediary form is designed to help simplify the algorithmthat analyzes said intermediary form easier). The back end of a compiler takes the intermediary form and then converts it to a 'target language'.
编译器由两个主要块组成:“前端”和“后端”。编译器的前端分析源代码并构建所述源代码的某种形式的“中间表示”,机器算法比源代码更容易分析(即,源代码,例如 C++ 被设计为帮助人类程序员编写代码,中间形式旨在帮助简化分析所述中间形式的算法更容易)。编译器的后端采用中间形式,然后将其转换为“目标语言”。
Now, the target language for general-use compilers are assembler languages for various processors, but there's nothing to prohibit a compiler back end to produce code in some other language, for as long as said target language is (at least) as flexible as a general CPU assembler.
现在,通用编译器的目标语言是各种处理器的汇编语言,但没有什么可以禁止编译器后端以其他语言生成代码,只要所述目标语言(至少)与通用 CPU 汇编程序。
Now, as you can probably imagine, C is definitely as flexible as a CPU's assembler, such that a C++ to C compiler is really no problem to implement from a technical pov.
现在,正如您可能想象的那样,C 绝对和 CPU 的汇编程序一样灵活,因此从技术观点来看,从 C++ 到 C 编译器的实现真的没有问题。
So you have: C++ ---frontEnd---> someIntermediaryForm ---backEnd---> C
所以你有: C++ ---frontEnd---> someIntermediaryForm ---backEnd---> C
You may want to check these guys out: http://www.edg.com/index.php?location=c_frontend(the above link is just informative for what canbe done, they license their front ends for tens of thousands of dollars)
你可能想看看这些人:http://www.edg.com/index.php?location= c_frontend(上面的链接只是提供了可以做什么的信息,他们以数万美元的价格许可他们的前端)
PS As far as i know, there is no such a C++ to C compiler by GNU, and this totally beats me (if i'm right about this). Because the C language is fairly small and it's internal mechanisms are fairly rudimentary, a C compiler requires something like one man-year work (i can tell you this first hand cause i wrote such a compiler myself may years ago, and it produces a [virtual] stack machine intermediary code), and being able to have a maintained, up-to-date C++ compiler while only having to write a C compiler once would be a great thing to have...
PS据我所知,GNU没有这样的C++到C编译器,这完全击败了我(如果我是对的)。因为 C 语言相当小,而且它的内部机制相当简陋,所以 C 编译器需要一个人年的工作(我可以直接告诉你,因为我可能在几年前自己写了这样一个编译器,它产生一个 [ virtual] 堆栈机中间代码),并且能够拥有一个维护的、最新的 C++ 编译器,同时只需要编写一次 C 编译器将是一件很棒的事情......
回答by Shafik Yaghmour
This is an old thread but apparently the C++ Faq
has a section(Archived 2013 version)on this. This apparently will be updated if the author is contacted so this will probably be more up to date in the long run, but here is the current version:
这是一个旧线程,但显然C++ Faq
有一个部分(存档 2013 版本)。如果联系作者,这显然会更新,因此从长远来看这可能会更新,但这是当前版本:
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:
- Comeau Computingoffers a compiler based on Edison Design Group's front endthat outputs C code.
- LLVMis a downloadable compiler that emits C code. See also hereand 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).
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.
取决于你的意思。如果您的意思是,是否可以将 C++ 转换为可读且可维护的 C 代码?那么抱歉,答案是否定的——C++ 特性不会直接映射到 C,而且生成的 C 代码不适合人类遵循。如果相反,您的意思是,是否有编译器将 C++ 转换为 C,以便编译到还没有 C++ 编译器的平台上?那么你很幸运——继续阅读。
将 C++ 编译为 C 的编译器对程序进行完整的语法和语义检查,并且恰好使用 C 代码作为生成目标代码的一种方式。这样的编译器不仅仅是某种花哨的宏处理器。(请不要给我发电子邮件,声称这些是预处理器——它们不是——它们是完整的编译器。)可以通过转换为 C 来实现 ISO 标准 C++ 的所有功能,并且除了异常处理之外,它通常会导致在目标代码中,其效率可与传统 C++ 编译器生成的代码相媲美。
以下是一些执行编译为 C 的产品:
- Comeau Computing提供基于Edison Design Group 前端的编译器,可输出 C 代码。
- LLVM是一个可下载的编译器,可以生成 C 代码。另请参阅此处和此处。这是通过 LLVM 将 C++ 转换为 C的示例。
Cfront是 C++ 的原始实现,由 Bjarne Stroustrup 和 AT&T 的其他人完成,生成 C 代码。但是它有两个问题:自从 90 年代中期开始经历所有权变更的迷宫以来,很难获得许可证,并且开发同时停止,因此它没有得到错误修复并且不支持任何较新的语言特性(例如,异常、命名空间、RTTI、成员模板)。
与流行的神话相反,在撰写本文时,还没有将 C++ 转换为 C 的 g++ 版本。这样的事情似乎是可行的,但我不知道有人真的做到了(还)。
请注意,您通常需要指定目标平台的 CPU、操作系统和 C 编译器,以便生成的 C 代码专门针对该平台。这意味着: (a) 您可能无法使用为平台 X 生成的 C 代码并在平台 Y 上编译它;(b) 自己进行翻译会很困难——使用其中一种工具可能会更便宜/更安全。
再一次:不要给我发电子邮件说这些只是预处理器——它们不是——它们是编译器。