Tool to convert (translate) C to Go? [closed]
Asked Answered
G

6

18

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.

Gavial answered 9/1, 2012 at 13:6 Comment(7)
I've not seen a tool matching your requirements, and this sounds more like a request to have one written for you, maybe not really a match for this site.Wasp
@JoachimIsaksson There already exist questions like "C to Java" or "C to Perl". This question is no different.Gavial
Why do you ask? Won't it be simpler to mix C & Go code? (GCC 4.7 has a Go front-end -still imperfect-, so that is possible sometimes)Felixfeliza
This site is about helping each other in a friendly way. I demand that all demands are downvoted :)Sororicide
I wrote a translator that can convert some simple C programs into Go, but it's only a proof-of-concept.Harmonize
This is a useful question and the fact that it's closed as off-topic is an unfortunate application of SO's rules.Cryobiology
The go compiler was written in C and the developers wrote a C-to-Go transpiler to convert the compiler into Go. See Go 1.3+ Compiler OverhaulBowes
F
4

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.

Felixfeliza answered 9/1, 2012 at 13:27 Comment(1)
Go also has a foreign function interface for calling C functions, so it should be possible to use C libraries in Go.Harmonize
C
16

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.

Cladophyll answered 7/10, 2012 at 17:52 Comment(1)
But how would you use c2go to translate a test.c program to a test.go program?Kenwood
F
4

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.

Felixfeliza answered 9/1, 2012 at 13:27 Comment(1)
Go also has a foreign function interface for calling C functions, so it should be possible to use C libraries in Go.Harmonize
D
4

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

Denote answered 30/3, 2017 at 14:58 Comment(0)
J
2

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 :)

Jeannettejeannie answered 9/1, 2012 at 22:44 Comment(0)
C
2

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.

Citronellal answered 22/3, 2014 at 16:11 Comment(0)
S
1

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.

Storage answered 9/1, 2012 at 13:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.