cxgo
Transpile C code to Go code
About cxgo
C to Go translator
CxGo is a tool for translating C source code to Go (aka transpiler, source-to-source compiler).
It uses cc v3 for preprocessing and parsing C (no clang/gcc dependencies!) and a custom type-checker and AST translation layer to make the best output possible.
The only requirement is: C code must compile with cxgo, including headers.
Having said that, cxgo uses a few tricks to make this process easier.
TL;DR for the project goals:
- Implement a practical C to Go translator (no C++ for now).
- Keep the output program code correct.
- Make the generated code human-readable and as idiomatic as possible.
- Make it easy to use and customize.
Check the FAQ for more common question about the project.
Status
The project is experimental! Do not rely on it in production and other sensitive environments!
Although it was successfully tested on multiple projects, it might change the behavior of the code due to yet unknown bugs.
Compiler test results:
- TCC: 62/89 (70%)
- GCC: 783/1236 (63%)
Transpiled projects:
- Potrace (image vectorization library)
- G722 (audio codec)
- PortableGL (OpenGL 3.x implementation)
- Physac (2D physics engine)
Installation
go install github.com/gotranspile/cxgo/cmd/cxgo@latest
or download the latest release from Github.
How to use
The fastest way to try it is:
cxgo file main.c
For more details, check our examples section.
It will guide you through basic usage patterns as well as a more advanced ones (on real-world projects).
You may also check FAQ if you have any issues.
Caveats
The following C features are currently accepted by cxgo, but may be implemented partially or not implemented at all:
- preserving comments from C code (#2)
static(#4)auto(#5)- bitfields (#6)
unionwith C-identical data layout (#7)packedstructs (#8)asmcasein weird places (#9)gotoforbidden by Go (there is a workaround, though, see #10)- label variables (#11)
- thread local storage (#12)
setjmp(will compile, but panics at runtime)- some stdlib functions and types are missing (good first issue!)
- deep type inference (when converting to Go string/slices)
- considering multiple
#ifdefpaths for different OS/envs
Community
Join our community! We'd like to hear back from you!
Contributing
See CONTRIBUTING.
License
Frequently Asked Questions
What is cxgo?
cxgo is a Go Compilers library for the Go programming language. Transpile C code to Go code
How do I install cxgo?
Install cxgo with the Go module system using `go get gotranspile/cxgo`. Check the repository for the current installation instructions.
What category does cxgo belong to?
cxgo is listed under Go Compilers, specifically Go Compilers.