{"id":13471897,"url":"https://github.com/elliotchance/c2go","last_synced_at":"2025-10-04T07:58:03.214Z","repository":{"id":46656753,"uuid":"85363897","full_name":"elliotchance/c2go","owner":"elliotchance","description":"⚖️ A tool for transpiling C to Go.","archived":false,"fork":false,"pushed_at":"2025-03-16T00:29:40.000Z","size":2172,"stargazers_count":2127,"open_issues_count":153,"forks_count":163,"subscribers_count":50,"default_branch":"master","last_synced_at":"2025-04-03T06:05:38.218Z","etag":null,"topics":["c","go","transpiler"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/elliotchance.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-03-17T23:53:26.000Z","updated_at":"2025-03-31T18:13:39.000Z","dependencies_parsed_at":"2022-08-31T11:52:25.959Z","dependency_job_id":"9c9cf26a-2faf-4a78-8436-044d59e4ad90","html_url":"https://github.com/elliotchance/c2go","commit_stats":{"total_commits":1065,"total_committers":36,"mean_commits":"29.583333333333332","dds":"0.38779342723004695","last_synced_commit":"5420ffae5e7929c73af40b0170df50b578d3d38f"},"previous_names":[],"tags_count":162,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/elliotchance%2Fc2go","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/elliotchance%2Fc2go/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/elliotchance%2Fc2go/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/elliotchance%2Fc2go/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/elliotchance","download_url":"https://codeload.github.com/elliotchance/c2go/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248202090,"owners_count":21064261,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["c","go","transpiler"],"created_at":"2024-07-31T16:00:50.133Z","updated_at":"2025-10-04T07:57:58.180Z","avatar_url":"https://github.com/elliotchance.png","language":"Go","readme":"[![Build Status](https://travis-ci.org/elliotchance/c2go.svg?branch=master)](https://travis-ci.org/elliotchance/c2go)\n[![GitHub version](https://badge.fury.io/gh/elliotchance%2Fc2go.svg)](https://badge.fury.io/gh/elliotchance%2Fc2go)\n[![Go Report Card](https://goreportcard.com/badge/github.com/elliotchance/c2go)](https://goreportcard.com/report/github.com/elliotchance/c2go)\n[![codecov](https://codecov.io/gh/elliotchance/c2go/branch/master/graph/badge.svg)](https://codecov.io/gh/elliotchance/c2go)\n[![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://raw.githubusercontent.com/elliotchance/c2go/master/LICENSE)\n[![Join the chat at https://gitter.im/c2goproject](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/c2goproject?utm_source=badge\u0026utm_medium=badge\u0026utm_campaign=pr-badge\u0026utm_content=badge)\n[![Twitter](https://img.shields.io/twitter/url/https/github.com/elliotchance/c2go.svg?style=social)](https://twitter.com/intent/tweet?text=Wow:\u0026url=%5Bobject%20Object%5D)\n[![GoDoc](https://godoc.org/github.com/elliotchance/c2go?status.svg)](https://godoc.org/github.com/elliotchance/c2go)\n\nA tool for converting C to Go.\n\nThe goals of this project are:\n\n1. To create a generic tool that can convert C to Go.\n2. To be cross platform (linux and mac) and work against as many clang versions\nas possible (the clang AST API is not stable).\n3. To be a repeatable and predictable tool (rather than doing most of the work\nand you have to clean up the output to get it working.)\n4. To deliver quick and small version increments.\n5. The ultimate milestone is to be able to compile the\n[SQLite3 source code](https://sqlite.org/download.html) and have it working\nwithout modification. This will be the 1.0.0 release.\n\n# Installation\n\n`c2go` requires Go 1.9 or newer.\n\n```bash\ngo install github.com/elliotchance/c2go@latest\n```\n\n# Usage\n\n```bash\nc2go transpile myfile.c\n```\n\nThe `c2go` program processes a single C file and outputs the translated code\nin Go. Let's use an included example,\n[prime.c](https://github.com/elliotchance/c2go/blob/master/examples/prime.c):\n\n```c\n#include \u003cstdio.h\u003e\n \nint main()\n{\n   int n, c;\n \n   printf(\"Enter a number\\n\");\n   scanf(\"%d\", \u0026n);\n \n   if ( n == 2 )\n      printf(\"Prime number.\\n\");\n   else\n   {\n       for ( c = 2 ; c \u003c= n - 1 ; c++ )\n       {\n           if ( n % c == 0 )\n              break;\n       }\n       if ( c != n )\n          printf(\"Not prime.\\n\");\n       else\n          printf(\"Prime number.\\n\");\n   }\n   return 0;\n}\n```\n\n```bash\nc2go transpile prime.c\ngo run prime.go\n```\n\n```\nEnter a number\n23\nPrime number.\n```\n\n`prime.go` looks like:\n\n```go\npackage main\n\nimport \"unsafe\"\n\nimport \"github.com/elliotchance/c2go/noarch\"\n\n// ... lots of system types in Go removed for brevity.\n\nvar stdin *noarch.File\nvar stdout *noarch.File\nvar stderr *noarch.File\n\nfunc main() {\n\t__init()\n\tvar n int\n\tvar c int\n\tnoarch.Printf([]byte(\"Enter a number\\n\\x00\"))\n\tnoarch.Scanf([]byte(\"%d\\x00\"), (*[1]int)(unsafe.Pointer(\u0026n))[:])\n\tif n == 2 {\n\t\tnoarch.Printf([]byte(\"Prime number.\\n\\x00\"))\n\t} else {\n\t\tfor c = 2; c \u003c= n-1; func() int {\n\t\t\tc += 1\n\t\t\treturn c\n\t\t}() {\n\t\t\tif n%c == 0 {\n\t\t\t\tbreak\n\t\t\t}\n\t\t}\n\t\tif c != n {\n\t\t\tnoarch.Printf([]byte(\"Not prime.\\n\\x00\"))\n\t\t} else {\n\t\t\tnoarch.Printf([]byte(\"Prime number.\\n\\x00\"))\n\t\t}\n\t}\n\treturn\n}\n\nfunc __init() {\n\tstdin = noarch.Stdin\n\tstdout = noarch.Stdout\n\tstderr = noarch.Stderr\n}\n```\n\n# How It Works\n\nThis is the process:\n\n1. The C code is preprocessed with clang. This generates a larger file (`pp.c`),\nbut removes all the platform-specific directives and macros.\n\n2. `pp.c` is parsed with the clang AST and dumps it in a colourful text format\nthat\n[looks like this](http://ehsanakhgari.org/wp-content/uploads/2015/12/Screen-Shot-2015-12-03-at-5.02.38-PM.png).\nApart from just parsing the C and dumping an AST, the AST contains all of the\nresolved information that a compiler would need (such as data types). This means\nthat the code must compile successfully under clang for the AST to also be\nusable.\n\n3. Since we have all the types in the AST it's just a matter of traversing the\ntree in a semi-intelligent way and producing Go. Easy, right!?\n\n# Testing\n\nBy default only unit tests are run with `go test`. You can also include the\nintegration tests:\n\n```bash\ngo test -tags=integration ./...\n```\n\nIntegration tests in the form of complete C programs that can be found in the\n[tests](https://github.com/elliotchance/c2go/tree/master/tests) directory.\n\nIntegration tests work like this:\n\n1. Clang compiles the C to a binary as normal.\n2. c2go converts the C file to Go.\n3. The Go is built to produce another binary.\n4. Both binaries are executed and the output is compared. All C files will\ncontain some output so the results can be verified.\n\n# Contributing\n\nContributing is done with pull requests. There is no help that is too small! :)\n\nIf you're looking for where to start I can suggest\n[finding a simple C program](http://www.programmingsimplified.com/c-program-examples)\n(like the other examples) that does not successfully translate into Go.\n\nOr, if you don't want to do that you can submit it as an issue so that it can be\npicked up by someone else.\n","funding_links":[],"categories":["开源类库","Go","Misc","Python Hacks","Open source library","By Target Language","Repositories"],"sub_categories":["开发辅助包","Development Aid Package","Golang"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Felliotchance%2Fc2go","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Felliotchance%2Fc2go","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Felliotchance%2Fc2go/lists"}