We don’t allow questions seeking recommendations for software libraries, tutorials, tools, books, or other off-site resources. You can edit the question so it can be answered with facts and citations.
Closed 5 years ago.
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(6)
确实有这样一个工具,Comeau的C++编译器。 。 它将生成您无法手动维护的 C 代码,但这没有问题。 您将维护 C++ 代码,并即时转换为 C。
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.
http://llvm.org/docs/FAQ.html#translatecxx
它处理一些代码,但对于更复杂的实现将会失败,因为它尚未针对某些现代 C++ 约定进行完全更新。 因此,请尝试经常编译代码,直到您了解允许的内容。
对于版本 9.0.1,命令行的使用语法如下:
对于旧版本(不确定过渡版本),请使用以下语法:
请注意,它创建了 GNU 风格的 C,而不是真正的 ANSI C。您将需要测试在您对代码投入过多之前,这对您很有用。 例如,某些嵌入式系统仅接受 ANSI C。
另请注意,它会生成功能正常但相当不可读的代码。 我建议注释并维护您的 C++ 代码,而不用担心最终的 C 代码。
编辑:虽然删除了对此功能的官方支持,但用户仍然可以使用 Julia 的此非官方支持语言开发人员,实现上述功能。
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:
For older versions (unsure of transition version), use the following syntax:
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.
EDIT : although official support of this functionality was removed, but users can still use this unofficial support from Julia language devs, to achieve mentioned above functionality.
虽然您可以在 C 中进行面向对象(例如,通过向方法添加
theType *this
第一个参数,并手动处理诸如虚表之类的多态性),但这作为设计从来都不是特别令人满意,而且看起来很丑(即使有一些预处理器黑客)。我建议至少考虑重新设计以比较其效果。
总体而言,很大程度上取决于关键问题的答案:如果您有可用的 C++ 代码,为什么需要 C?
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).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?
也许好的 cfront 可以吗?
Maybe good ol' cfront will do?
编译器由两个主要块组成:“前端”和“后端”。
编译器的前端分析源代码并构建所述源代码的某种形式的“中间表示”,该源代码比源代码更容易通过机器算法进行分析(即,而源代码,例如 C++ 被设计为帮助人类程序员编写代码,中间形式旨在帮助简化分析所述中间形式的算法(更容易)。
编译器的后端采用中间形式,然后将其转换为“目标语言”。
现在,通用编译器的目标语言是用于各种处理器的汇编语言,但是没有什么可以禁止编译器后端以其他语言生成代码,只要所述目标语言(至少)与通用CPU汇编器。
现在,正如您可能想象的那样,C 绝对与 CPU 的汇编器一样灵活,因此从技术角度来看,实现 C++ 到 C 的编译器确实没有问题。
所以你有:C++ ---frontEnd---> someIntermediaryForm ---backEnd---> C
您可能想看看这些人: http://www.edg.com/index .php?location=c_frontend
(上面的链接只是提供了可以可以做什么的信息,他们以数万美元的价格授权其前端)
PS
据我所知,GNU 没有这样的 C++ 到 C 编译器,这完全打败了我(如果我是对的)。 因为 C 语言相当小,而且它的内部机制也相当初级,所以 C 编译器需要大约一个人年的工作(我可以第一手告诉你,因为我自己可能几年前编写了这样一个编译器,它产生了 [虚拟]堆栈机中间代码),并且能够拥有一个维护的、最新的 C++ 编译器,同时只需编写一次 C 编译器,这将是一件很棒的事情......
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 programmer to write code, the intermediary form is designed to help simplify the algorithm that analyzes said intermediary form easier).
The back end of a compiler takes the intermediary form and then converts it to a 'target language'.
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.
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.
So you have: 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 can be done, they license their front ends for tens of thousands of dollars)
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...
这是一个旧线程,但显然
C++ Faq
有一个 部分 (2013 年存档版本) 对此。 如果联系作者,这显然会更新,因此从长远来看这可能会更新,但这是当前版本: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: