2

Can someone point out a Go to C compiler to me, or does indeed none exist? A lengthy web search turned up nothing for me.

In particular, I am looking for an open source Go-to-C compiler that can be built for Linux.

  • What is your goal? Why do you want to compile to c? What are you missing from the default way? – guettli Dec 1 '15 at 6:47
  • @guettli: I want to compile to C because this way I get the best cross-platform support. Not sure what your last question means, though. Also it's besides the point, since this SE site is for software recommendations. I would have posted it to StackOverflow otherwise :) – 0xC0000022L Dec 2 '15 at 9:58
  • which platform is your target? – guettli Dec 2 '15 at 13:03
  • @guettli: Linux, as the subject mentions. Or you mean the ultimate targets for which I want to compile the resulting C code? Those would include Solaris, AIX, several of the BSDs on architectures such as x86_32, x86_64, PPC, SPARC and MIPS. – 0xC0000022L Dec 3 '15 at 16:01
  • I looked at this page: golang.org/doc/install/source there is a gcc backend for go. I guess all your platforms are supported by gcc. Why not compile go for these platforms? – guettli Dec 3 '15 at 20:26
2

If you want the greatest cross platform support with least effort then Go is a great choice.

While there may be some C compilers for a few platforms that Go does not support, C is actually not nearly as cross platform friendly as you'd think (or was originally hopped).

In particular the Go standard library is built so that it supports the platforms that Go supports by including and automatically switching between platform specific implementations. These alternate implementations are automatically selected for you by the Go compiler at build time.

C has no equivalent mechanism. Which underscores why this was an important feature for Go's multi-platform and cross compilation support.

For example, consider the path/filepath package in Go. It automatically switches between backward or forward slashes ("\","/") when working with paths depending on if the the compile target is Windows or not.

This mechanism is lost if you were to first compile Go to C then compile on the target platform, because at Go to C compile time the target platform is still unknown. This means that contrary to your expectations, cross compiling to C would reduce Go's cross platform support, not improve it.


All of that being said, you may want to check out the bblfsh project:

https://github.com/bblfsh/bblfshd

https://doc.bblf.sh

Bblfsh is essentially an Abstract Syntax Tree cross compiler. There are C++ an Go drivers for Bblfsh, but I don't see one for C. One can compile directly from the language AST, or even write the source code that would generate the AST.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.