将C代码转换(翻译)为Go的工具?

huangapple go评论89阅读模式
英文:

Tool to convert (translate) C to Go?

问题

要将C源代码转换为Go源代码,可以使用哪个工具?

例如,如果C代码包含:

struct Node {
    struct Node *left, *right;
    void        *data;
};

char charAt(char *s, int i) {
    return s[i];
}

工具生成的相应Go代码应为:

type Node struct {
    left, right *Node
    data        interface{}
}

func charAt(s string, i int) byte {
    return s[i]
}

该工具不需要完美。如果生成的Go代码的某些部分需要手动更正,也是可以的。

英文:

What tool to use to convert C source code into Go source code?

For example, if the C code contains:

struct Node {
    struct Node *left, *right;
    void        *data;
};

char charAt(char *s, int i) {
    return s[i];
}

the corresponding Go code generated by the tool should be:

type Node struct {
    left, right *Node
    data        interface{}
}

func charAt(s string, i int) byte {
    return s[i]
}

The tool does not need to be perfect. It is OK if some parts of the generated Go code need to be corrected by hand.

答案1

得分: 16

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.

答案2

得分: 4

我猜今天没有这样的(从C到Go源代码转换)工具存在。你可以考虑制作自己的转换器。问题是:这值得吗,如何做到呢?

可能不值得这样做,因为Go和C可能在某种程度上是可互操作的。例如,如果你使用GCC 4.6(或即将发布的4.7,即最新快照),你可能可以将C和Go代码链接在一起,只需一些小心。

当然,像往常一样,魔鬼在细节中。

如果你想要一个转换器,你希望得到的Go代码可读和可编辑吗(那么任务就更困难了,因为你想保留代码的结构,还想保留注释)?在这种情况下,你可能需要自己的C解析器(这是一个困难的任务)。

如果你不关心生成的Go代码的可读性,你可以扩展现有的编译器来完成工作。例如,GCC可以通过插件或MELT扩展进行扩展,你可以定制GCC(使用MELT或自己的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.

<sup>Of course, as usual, the evil is in the details.</sup>

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.

答案3

得分: 4

查看这个项目

https://github.com/elliotchance/c2go

详细描述在这篇文章

更新: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

答案4

得分: 2

我几乎确定没有这样的工具,但在我看来,用每种语言的自己的“编码风格”编写代码是很好的。

还记得我们多么喜欢C预处理器的技巧和指针的艺术性工作吗?还记得处理malloc/free或线程时需要多么小心吗?
Go是不同的。你没有预处理器,但你有闭包、带有方法的对象、接口、垃圾回收器、切片、goroutine和许多其他好的特性。

那么,为什么要转换代码而不是以更好、更清晰的方式重写它呢?

当然,我希望你不会有1000K行的C代码需要转换到Go 将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 将C代码转换(翻译)为Go的工具?

答案5

得分: 2

看一下SWIG,http://www.swig.org/Doc2.0/Go.html 它会将C/C++头文件翻译成Go语言并进行封装,作为一个起点。然后你可以逐步移植部分代码。

英文:

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.

答案6

得分: 1

据我所知,目前还没有这样的工具。所以你必须手动将你的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.

huangapple
  • 本文由 发表于 2012年1月9日 21:06:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/8788866.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定