百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
C

c4go

> 编程语言
开源

将 C 代码翻译成 Go 代码

375 stars0 点赞1 次浏览
访问官网GitHub

工具介绍

将 C 代码翻译成 Go 代码

A tool for transpiling C code to Go code.

Milestones of the project:

  1. Transpiling project GNU GSL.
  2. Transpiling project GTK+.

Notes:

  • This transpiler works on all linux machines. Most likely, it will work on Unix and MacOS as well. Feedback is most welcome.
  • Installing the clang compiler is a prerequisite. See llvm download page

Installation

Using a terminal window, position into a working directory (E.g. $HOME/development). Then, execute the following shell commands:

bash
# Create a local c4go repository and position into it.
git clone https://github.com/Konstantin8105/c4go
cd c4go

# Update the Go source files.
go generate ./...

# Install the c4go executable in the standard Go bin directory: $HOME/go/bin.
go install

# Make sure that $HOME/go/bin is in the path (good practice).
# Then, verify that the c4go is available for use.
c4go version

Sample c4go version console output:

Build time: May  4 20:41:31 2024 UTC
Git hash:   "c5400a76df0f5157f234547bef3ae8fb7b692685"

Usage example

bash
# Change your location to the folder with examples:
cd $GOPATH/src/github.com/Konstantin8105/c4go/examples/

# Transpile one file from the C example folder:
c4go transpile prime.c

# Look at the result
nano prime.go

# Check the result:
go run prime.go
# Enter a number
# 13
# The number is: 13
# Prime number.

C code of file prime.c:

c
#include 

int main()
{
    int n, c;

    printf("Enter a number\n");
    // get value
    scanf("%d", &n);
    printf("The number is: %d\n", n);

    // -------
    if (n == 2)
        printf("Prime number.\n");
    else {
        for (c = 2; c 
#include 

int main()
{
    int n;
    double param = 8.0, result;
    result = frexp(param, &n);
    printf("result = %5.2f\n", result);
    printf("n      = %d\n", n);
    return 0;
}

c4go add automatically C binding for function without implementation:

…

Example with C-pointers and C-arrays

…
…

Example of transpilation neatvi.

bash
# clone git repository
git clone https://github.com/aligrudi/neatvi.git

# move in folder
cd neatvi

# look in Makefile
cat Makefile

Makefile:

bash
CC = cc
CFLAGS = -Wall -O2
LDFLAGS =

OBJS = vi.o ex.o lbuf.o mot.o sbuf.o ren.o dir.o syn.o reg.o led.o \
	uc.o term.o rset.o regex.o cmd.o conf.o

all: vi

conf.o: conf.h

%.o: %.c
	$(CC) -c $(CFLAGS) $ int}. err = Cannot resolve type '__pid_t' : I couldn't find an appropriate Go type for the C type '__pid_t'.

For fix that add flag -s for adding all type from C standard library:

bash
c4go transpile -o vi.go -s *.c

Now, all is Ok. Look the result:

bash
less vi.go

C standard library implementation

…

Contributing

Feel free to submit PRs or open issues. Main information from: en.cppreference.com

Testing

By default, only unit tests are run with go test. You can also include all tests:

bash
go test ./...

For tests work like this:

  1. clang compiles the C to a binary as normal.
  2. c4go converts the C file to Go.
  3. Both binaries are executed and the output is compared. All C files will contain some output so the results can be verified.

Note

Use lastest version of clang.

If you use Ubuntu, then use command like next for choose clang version:

bash
sudo update-alternatives --install /usr/bin/clang clang /usr/bin/clang-6.0 1000

Performance

Main time of transpilation takes clang, for example run:

bash
go test -run=Benchmark -bench=. -benchmem

Result looks for example:

goos: linux
goarch: amd64
pkg: github.com/Konstantin8105/c4go
BenchmarkTranspile/Full-6         	       5	 274922964 ns/op	43046865 B/op	  379676 allocs/op
BenchmarkTranspile/GoCode-6       	      20	  86806808 ns/op	36577533 B/op	  308060 allocs/op
PASS

So, transpilation time is just 30% of full time. In my point of view no need of performance optimization, see Amdahl's law.

Example of performance analyse

Please run:

…

For more information, see Profiling Go Programs.

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Ccc4goconvertcpp

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言