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 7 years ago.
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(6)
rsc 创建了 github.com/rsc/c2go 将基于 c 的 Go 编译器转换为 Go。
作为外部示例,akavel 似乎试图使用它来创建基于 Go 的 lua: github.com/akavel/ goluago/
github.com/xyproto/c2go 是另一个项目,但还没有感动一会儿。
rsc created github.com/rsc/c2go to convert the c based Go compiler into Go.
As an external example, akavel seems to be trying to use it to create a Go based lua: github.com/akavel/goluago/
github.com/xyproto/c2go is another project, but it hasn't been touched in a little while.
我想现在不存在这样的(C 到 Go 源代码转换)工具。您可能会考虑制作自己的转换器。问题是:这是否值得,以及如何做到这一点?
这可能不值得付出努力,因为 Go 和 C 可以以某种方式实现互操作。例如,如果您使用GCC 4.6(或即将发布的4.7,即最新快照)您可能可以链接C&一起编写代码,但要小心。
当然,一如既往,细节决定成败。
如果你想要一个转换器,你是否希望获得的 Go 代码可读可编辑(那么任务就更困难了,因为你想要保留代码的结构,并且您还想保留注释)?在这种情况下,您可能需要自己的 C 解析器(这是一项艰巨的任务)。
如果您不关心生成的 Go 代码的可读性,您可以扩展现有的编译器来完成这项工作。例如,GCC 可以通过插件或 MELT 扩展进行扩展,并且您可以自定义 GCC(使用MELT,或者您自己的 GCC C 插件)将 Gimple 表示(GCC 内部指令的主要内部表示)转换为不可读的 Go 代码。这在某种程度上更简单(但仍然需要一周以上的工作)。
当然,Go 接口、通道甚至内存管理(垃圾收集内存)都没有标准的 C 对应项。
I guess no such (C to Go source code conversion) tool exist today. You might consider to make your own converter. The question becomes: is it worth it, and how to do that?
It probably might not be worth the effort, because Go and C could be somehow interoperable. For example, if you use the GCC 4.6 (or to be released 4.7, i.e. the latest snapshot) your probably can link C & Go code together, with some care.
Of course, as usual, the evil is in the details.
If you want a converter, do you want the obtained Go code to be readable and editable (then the task is more difficult, since you want to keep the structure of the code, and you also want to keep the comments)? In that case, you probably need your own C parser (and it is a difficult task).
If you don't care about readability of the generated Go code, you could for example extend an existing compiler to do the work. For example, GCC is extensible thru plugins or thru MELT extensions, and you could customize GCC (with MELT, or your own C plugin for GCC) to transform Gimple representation (the main internal representation for instructions inside GCC) to unreadable Go code. This is somehow simpler (but still require more than a week of work).
Of course, Go interfaces, channels and even memory management (garbage collected memory) has no standard C counterpart.
查看这个项目
https://github.com/elliotchance/c2go
详细说明在此< a href="http://elliot.land/post/converting-c-to-go?utm_source=golangweekly&utm_medium=email" rel="nofollow noreferrer">文章
更新:2021 年 8 月 6 日
另请查看这篇文章
https ://github.com/gotranspile/cxgo
Check out this project
https://github.com/elliotchance/c2go
The detailed description is in this article
Update: August 6, 2021
Also check this one
https://github.com/gotranspile/cxgo
我几乎可以肯定没有这样的工具,但恕我直言,在每种语言中,用自己的“编码风格”编写都是很好的。
还记得我们多么喜欢 C 预处理器技巧和指针的真正艺术作品吗?还记得处理 malloc/free 或线程需要多么小心吗?
围棋则不同。你没有预处理器,但你有闭包、带有方法的对象、接口、垃圾收集器、切片、goroutines 和许多其他好的功能。
那么,为什么要转换代码而不是以更好、更简洁的方式重写代码呢?
当然,我希望你没有 1000K 行 C 代码需要移植到 Go :)
I'm almost sure there is no such tool, but IMHO in every language it's good to write in its own "coding style".
Remember how much we all loved C preprocessor tricks and really artistic work with pointers? Remember how much care it took to deal with malloc/free or with threads?
Go is different. You have no preprocessor, but you have closures, objects with methods, interfaces, garbage collector, slices, goroutines and many other nice features.
So, why to convert code instead of rewriting it in a much better and cleaner way?
Of course, I hope you don't have a 1000K lines of code in C that you have to port to Go :)
看看 SWIG,http://www.swig.org/Doc2.0/Go .html 它将转换 C/C++ 标头并将其包装为起点。然后你可以一点一点地移植零件。
Take a look at SWIG, http://www.swig.org/Doc2.0/Go.html it will translate the C/C++ headers to go and wrap them for a starting point. Then you can port parts over bit by bit.
据我所知,这样的工具还不存在。因此,您必须手动将 C 代码转换为 Go。
我不知道您想要转换的 C 代码有多复杂,但您可能需要记住 Go 有一种“特殊”的处理方式。就像接口和频道。
As far as I know, such tool does not exist (yet). So you're bound to convert your C code to Go by hand.
I don't know how complex the C code is you want to convert, but you might want to keep in mind Go has a "special" way of doing things. Like the usage of interfaces and channels.