Transpiling C code to Go code
A tool for transpiling C code to Go code.
Milestones of the project:
Notes:
clang compiler is a prerequisite. See llvm download pageUsing a terminal window, position into a working directory (E.g. $HOME/development). Then, execute the following shell commands:
# 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"
# 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:
#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:
…
…
…
# clone git repository
git clone https://github.com/aligrudi/neatvi.git
# move in folder
cd neatvi
# look in Makefile
cat Makefile
Makefile:
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:
c4go transpile -o vi.go -s *.c
Now, all is Ok. Look the result:
less vi.go
…
Feel free to submit PRs or open issues. Main information from: en.cppreference.com
By default, only unit tests are run with go test. You can also include
all tests:
go test ./...
For tests work like this:
clang compiles the C to a binary as normal.c4go converts the C file to Go.If you use Ubuntu, then use command like next for choose clang version:
sudo update-alternatives --install /usr/bin/clang clang /usr/bin/clang-6.0 1000
Main time of transpilation takes clang, for example run:
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.
Please run:
…
For more information, see Profiling Go Programs.
No open issues yet, or sync has not completed.