Comments (8)
At the moment the project seems to not be working with recent versions of clang. Even a simple helloworld.c fails to transpile:
Source C
$ cat main.c
#include <stdio.h>
int main() {
// printf() displays the string inside quotation
printf("Hello, World!");
return 0;
}
Run the command to convert it into Go code:*
c2go transpile main.c
Error ...
panic: unknown node type: 'ColdAttr 0x7f9fe58af708 <col:42>'
goroutine 76 [running]:
github.com/elliotchance/c2go/ast.Parse({0xc00032893a, 0x20})
/Users/drpaneas/gocode/pkg/mod/github.com/elliotchance/[email protected]/ast/ast.go:294 +0x22d4
main.convertLinesToNodes({0xc000333d40, 0xe, 0x0})
/Users/drpaneas/gocode/pkg/mod/github.com/elliotchance/[email protected]/main.go:89 +0x115
main.convertLinesToNodesParallel.func1.2({0xc000333d40, 0x0, 0x0}, 0x0)
/Users/drpaneas/gocode/pkg/mod/github.com/elliotchance/[email protected]/main.go:121 +0x39
created by main.convertLinesToNodesParallel.func1
/Users/drpaneas/gocode/pkg/mod/github.com/elliotchance/[email protected]/main.go:119 +0x2da
After a couple hours of debugging I found out that the most recent supported version of clang seems to be:
11.0.0 (https://github.com/llvm/llvm-project.git 0160ad802e899c2922bc9b29564080c22eb0908c)
Since I don't want to mess up my system with different clang versions, I took a docker image that comes with clang 11 (see https://hub.docker.com/r/silkeh/clang/tags) and then I've installed Go and c2go on top. In that way it works in any machine, without worrying about the local system dependencies (well, you need docker obviously).
The dockerfile:
FROM docker.io/silkeh/clang:11
ENV DEBIAN_FRONTEND=noninteractive
RUN apt-get update && \
apt-get -o Dpkg::Options::="--force-confold" upgrade -q -y --force-yes && \
apt-get -o Dpkg::Options::="--force-confold" dist-upgrade -q -y --force-yes \
&& apt-get install golang-go -q -y --force-yes \
&& mkdir -p ~/go/{bin,src,pkg} \
&& mkdir /code && mkdir /gocode
ENV GOPATH="$HOME/go"
ENV GOBIN="$GOPATH/bin"
ENV GO111MODULE="on"
ENV PATH=$PATH:$GOBIN
RUN go get -u github.com/elliotchance/c2go \
&& echo "#!/usr/bin/env bash" > /gocode/script.sh \
&& echo "\$GOBIN/c2go transpile /code/\$1" >> /gocode/script.sh \
&& echo "NAME=\$(basename \$1 .c)" >> /gocode/script.sh \
&& echo "cat /\$NAME.go" >> /gocode/script.sh \
&& chmod +x /gocode/script.sh
ENTRYPOINT [ "/gocode/script.sh" ]
You can run it like this:
$ docker run --rm -it -v $PWD:/code docker.io/drpaneas/c2go:v0.26.0 main.c
The output now works fine, e.g. see the main()
function of the helloworld example (the one that failed earlier):
// removed C types for brevity (it produces a long file)
func main() {
noarch.Printf((&[]byte("Hello, World!\x00")[0]))
return
}
func init() {
stdin = noarch.Stdin
stdout = noarch.Stdout
stderr = noarch.Stderr
}
For shortcut (so you don't have to remember the long train-like docker command) you can put a function in our bash/zshell like this (e.g. vim ~/.zshenv
or vim ~/.bashrc
):
c2go() {
docker run --rm -it -v $PWD:/code docker.io/drpaneas/c2go:v0.26.0 $1
}
then source your file (e.g. source ~/.zshenv
or source ~/.bashrc
) and try again passing only the filename you want to transpile: c2go main.c
or ``c2go main.c > main.go` (if you want to put it into a file)
Now it works like a charm :)
from c2go.
@drpaneas you are an absolute hero! I may finally be able to play around with this tool!
from c2go.
The answer is the Dockerfile which I have posted earlier. You can just copy paste it and build it locally. I've literally described the whole process in my previous step.
from c2go.
Not that I know of, but itβs a good idea.
from c2go.
@elliotchance Gotcha! I can take a stab at it, though I'm far from a docker expert. Are there particular versions of clang that you use on your system that you know for sure work with the test examples?
from c2go.
It been so long since I've worked on c2go that I don't remember which versions I was using.
from c2go.
@drpaneas I'm kinda new to docker. My question is how did you get go and then have c2go install in your container ? any help on this. I really would like to get c2go to work.
from c2go.
@drpaneas thanks for the reply. I did the process you described and got the following error. like I said I'm new to docker. but will love to learn:
=> ERROR [2/3] RUN apt-get update && apt-get -o Dpkg::Options::="--force-confold" upgrade -q -y --force-yes && apt-get -o Dpkg::Options::="- 3.7s
[2/3] RUN apt-get update && apt-get -o Dpkg::Options::="--force-confold" upgrade -q -y --force-yes && apt-get -o Dpkg::Options::="--force-confold" dist-upgrade -q -y --force-yes && apt-get install golang-go -q -y --force-yes && mkdir -p
/go/{bin,src,pkg} && mkdir /code && mkdir /gocode:1).
#5 0.342 Hit:1 http://deb.debian.org/debian bullseye InRelease
#5 0.342 Get:2 http://security.debian.org/debian-security bullseye-security InRelease [44.1 kB]
#5 0.365 Hit:3 http://deb.debian.org/debian bullseye-updates InRelease
#5 0.654 Fetched 44.1 kB in 0s (103 kB/s)
#5 0.654 Reading package lists...
#5 1.152 Reading package lists...
#5 1.629 Building dependency tree...
#5 1.772 Reading state information...
#5 1.798 Calculating upgrade...
#5 1.984 W: --force-yes is deprecated, use one of the options starting with --allow instead.
#5 1.984 0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
#5 1.997 Reading package lists...
#5 2.473 Building dependency tree...
#5 2.614 Reading state information...
#5 2.643 Calculating upgrade...
#5 2.872 W: --force-yes is deprecated, use one of the options starting with --allow instead.
#5 2.873 0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
#5 2.885 Reading package lists...
#5 3.365 Building dependency tree...
#5 3.504 Reading state information...
#5 3.693 W: --force-yes is deprecated, use one of the options starting with --allow instead.
#5 3.693 golang-go is already the newest version (2:1.15
#5 3.693 0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
#5 3.698 mkdir: cannot create directory '/code': File exists
executor failed running [/bin/sh -c apt-get update && apt-get -o Dpkg::Options::="--force-confold" upgrade -q -y --force-yes && apt-get -o Dpkg::Options::="--force-confold" dist-upgrade -q -y --force-yes && apt-get install golang-go -q -y --force-yes && mkdir -p ~/go/{bin,src,pkg} && mkdir /code && mkdir /gocode]: exit code: 1
from c2go.
Related Issues (20)
- panic: unknown node type: 'MSAllocatorAttr 0x21a234729f0 <line:420:25>' HOT 1
- How to handle dependencies? HOT 2
- panic: could not match regexp with string HOT 1
- panic: unknown node type: 'CallbackAttr 0x562b0bc30c88 <line:199:12> Implicit 2 3'
- Cannot tranpile (sic)
- panic: unknown node type: ColdAttr HOT 1
- __int128 is translated as int64
- panic: unknown node type: BuiltinAttr HOT 1
- panic: unknown node type: 'BuiltinAttr 0x7fb08f04f838 <<invalid sloc>> Inherited Implicit 720' HOT 2
- panic: could not match regexp with string (translation_unit_decl) HOT 2
- 'const' removed from struct name / cannot determine sizeof union HOT 2
- panic: runtime error: invalid memory address or nil pointer dereference
- An alternative to github.com/elliotchance/c2go
- panic: unknown node type: 'BuiltinAttr 0x10db7b0 <<invalid sloc>> Inherited Implicit 749'
- FreeBSD implementation
- panic: unknown node type: 'BuiltinAttr 0x1d448112da0 <<invalid sloc>> Implicit 836' HOT 3
- Installation of c2go HOT 3
- Is it possible to convert .cpp aswell? HOT 1
- Compile `pikchr` source code
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. πππ
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from c2go.