https://github.com/gotranspile/cxgo
Tool for transpiling C to Go.
https://github.com/gotranspile/cxgo
c go transpiler
Last synced: 2 months ago
JSON representation
Tool for transpiling C to Go.
- Host: GitHub
- URL: https://github.com/gotranspile/cxgo
- Owner: gotranspile
- License: mit
- Created: 2021-01-10T17:08:53.000Z (almost 5 years ago)
- Default Branch: main
- Last Pushed: 2025-03-18T09:43:27.000Z (9 months ago)
- Last Synced: 2025-10-19T23:45:04.165Z (2 months ago)
- Topics: c, go, transpiler
- Language: Go
- Homepage: https://gotranspile.dev
- Size: 376 KB
- Stars: 372
- Watchers: 6
- Forks: 24
- Open Issues: 36
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
- awesome-go - cxgo - Transpile C code to Go code. (Go Compilers / Search and Analytic Databases)
- awesome-go-with-stars - cxgo - Transpile C code to Go code. (Go Compilers / Search and Analytic Databases)
- awesome-go - gotranspile/cxgo
- awesome-go - cxgo - Transpile C code to Go code. (Go Compilers / Search and Analytic Databases)
- fucking-awesome-go - cxgo - Transpile C code to Go code. (Go Compilers / Search and Analytic Databases)
README
# C to Go translator
[](https://raw.githubusercontent.com/gotranspile/cxgo/master/LICENSE)
[](https://gitter.im/gotranspile/community?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge)
[](https://pkg.go.dev/github.com/gotranspile/cxgo)
[](https://github.com/nikolaydubina/go-recipes)
CxGo is a tool for translating C source code to Go (aka transpiler, source-to-source compiler).
It uses [cc v3](https://modernc.org/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](CONTRIBUTING.md#project-goals-and-principles):**
1. Implement a practical C to Go translator ([no C++](https://github.com/gotranspile/cxgo/issues/1) for now).
2. Keep the output program code correct.
3. Make the generated code human-readable and as idiomatic as possible.
4. Make it easy to use and customize.
Check the [FAQ](FAQ.md) 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](https://github.com/gotranspile/gotrace) (image vectorization library)
- [G722](https://github.com/gotranspile/g722) (audio codec)
- [PortableGL](https://github.com/TotallyGamerJet/pgl) (OpenGL 3.x implementation)
- [Physac](https://github.com/koteyur/physac-go) (2D physics engine)
## Installation
```bash
go install github.com/gotranspile/cxgo/cmd/cxgo@latest
```
or download the [latest release](https://github.com/gotranspile/cxgo/releases/latest) from Github.
## How to use
The fastest way to try it is:
```bash
cxgo file main.c
```
For more details, check our [examples](./examples/README.md) 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](FAQ.md) 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](https://github.com/gotranspile/cxgo/issues/2))
- `static` ([#4](https://github.com/gotranspile/cxgo/issues/4))
- `auto` ([#5](https://github.com/gotranspile/cxgo/issues/5))
- bitfields ([#6](https://github.com/gotranspile/cxgo/issues/6))
- `union` with C-identical data layout ([#7](https://github.com/gotranspile/cxgo/issues/7))
- `packed` structs ([#8](https://github.com/gotranspile/cxgo/issues/8))
- `asm`
- `case` in weird places ([#9](https://github.com/gotranspile/cxgo/issues/9))
- `goto` forbidden by Go (there is a [workaround](docs/config.md#identsflatten), though, see [#10](https://github.com/gotranspile/cxgo/issues/10))
- label variables ([#11](https://github.com/gotranspile/cxgo/issues/11))
- thread local storage ([#12](https://github.com/gotranspile/cxgo/issues/12))
- `setjmp` (will compile, but panics at runtime)
- some stdlib functions and types are missing ([good first issue!](CONTRIBUTING.md#adding-a-new-known-header))
- deep type inference (when converting to Go string/slices)
- considering multiple `#ifdef` paths for different OS/envs
## Community
Join our [community](COMMUNITY.md)! We'd like to hear back from you!
## Contributing
See [CONTRIBUTING](CONTRIBUTING.md).
## License
[MIT](LICENSE)