Programming languages that compile into C/C++ source?

C++CProgramming LanguagesCode Translation

C++ Problem Overview


I'm using CoffeeScript to make JavaScript development easier. It's a language with clean syntax that compiles into JavaScript.

So, what are the established programming languages that compile into C/C++ source code, to simplify syntax and/or enhance functionality?

C++ Solutions


Solution 1 - C++

The language Haxe can output to C++, C#, Java, JavaScript, Python(experimental), PHP, Flash and NekoVM.

Solution 2 - C++

Vala and Genie are languages that use the GObject type system and compile to C code. I've never used them but they look interesting. GObject is the type system used by GTK but I believe it's separable from GTK.

Solution 3 - C++

GHC (the Glasgow Haskell Compiler) used to have an option (-fvia-c) to compile to C.

I believe that starting with v7 however, LLVM is used to generate native code instead of going via an external C compiler.

Solution 4 - C++

OOC is very new but quite nice.

Solution 5 - C++

There's HipHop, which transforms PHP programs to C++.

Solution 6 - C++

Quoting Qt documentation:

> The Meta-Object Compiler, moc, is the program that handles Qt's C++ extensions. The moc tool reads a C++ header file. If it finds one or more class declarations that contain the Q_OBJECT macro, it produces a C++ source file containing the meta-object code for those classes. Among other things, meta-object code is required for the signals and slots mechanism, the run-time type information, and the dynamic property system. The C++ source file generated by moc must be compiled and linked with the implementation of the class.

So it's kind of C++ getting turned into C++, I'm not sure if it fits in what you are looking for.

Solution 7 - C++

The programming language IBM Informix 4GL goes through a multi-step transformation to C code, which is then compiled and linked with its own runtime library, the ESQL/C (Embedded SQL in C) runtime libraries, and the system's C libraries.

  • program.4gl - original source code
  • program.4ec - C code with embedded ESQL/C and extended input operations
  • program.ec - C code with embedded ESQL/C
  • program.c - pure C code
  • program.o - object code
  • program - executable

The .4ec phase is a sort of historical accident; originally, the ESQL/C compiler handled both the extended input operations and the ESQL/C, but when the extended operations were removed from the ESQL/C compiler (when the ESQL/C library and compiler was significantly rewritten), the extended input operations were handled by carving out the code that handled that from the original ESQL/C compiler.

A fair number of people have written code generators which write I4GL source from another language, or which preprocess a closely related source code into I4GL before it is submitted to the I4GL compiler.

Solution 8 - C++

Embeddable Common Lisp (ECL) can compile Common Lisp to C.

Solution 9 - C++

I suppose you could write a C backend for LLVM, then you could translate pretty much anything down to C, but the deeper question would be "why?" The reason that there aren't any tools that do this today is that if you are going to compile down to C, why not go all the way and target some intermediate machine code language instead? (For example, LLVM or the JVM)

Now in the older days, the idea of compiling down to C was a bit more defensible, since there was a lack of decent vm languages, but today there are not too many good reasons to do it. That said, you can still find plenty of projects around which take certain interpreted languages and try to compile them down to C/C++. For example, here is a python to C++ compiler:

http://shed-skin.blogspot.com/

Solution 10 - C++

One could argue that since most major programming languages are Turing-complete, they are actually equivalent and programs written in them can be translated into each other.

That said, as other people have mentioned, there are quite a few languages for which there are (or used to be, early in their development) backends that produce C code, since that removed the complexities involved in binary code generation from the language implementation. That does not by any means mean (pun unintended) that said code was actually readable - it was just more readable than its compiled form.

As for my contribution to the list, lex and yacc "programs" (if they can be considered that) are typically transformed into C code - a horrible, tangled mess of it, but C code nonetheless...

Solution 11 - C++

I hear that clang can compile C++ into C. I doubt that would "simplify" anything, though.

Solution 12 - C++

Oracle's PRO*C/C++ is an embedded SQL language. It allows the inclusion of SQL statements in C/C++ which is processed by a precompiler that replaces the embedded SQL statements with function calls to the needed C/C++ SQL libraries. The output from the precompiler is standard C/C++ which can then be compiled to an executable.

http://en.wikipedia.org/wiki/Pro*C
http://download.oracle.com/docs/cd/B19306_01/appdev.102/b14407/toc.htm

Solution 13 - C++

A subset of Matlab (Embedded Matlab) can be compiled to C. You need the embedded coder toolbox.

Solution 14 - C++

Most languages can be used to generate C code, but it really depends what you're trying to do. Do you actually want to be able to read the code? Are you just trying to create Windows applications?

If you're looking for a universally useful language that's easy to learn, Python is always a good choice, and it can do everything that C/C++ can.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJeffView Question on Stackoverflow
Solution 1 - C++M.StoffregenView Answer on Stackoverflow
Solution 2 - C++SeanView Answer on Stackoverflow
Solution 3 - C++stusmithView Answer on Stackoverflow
Solution 4 - C++tsgView Answer on Stackoverflow
Solution 5 - C++GWWView Answer on Stackoverflow
Solution 6 - C++Vinicius KamakuraView Answer on Stackoverflow
Solution 7 - C++Jonathan LefflerView Answer on Stackoverflow
Solution 8 - C++sigjuiceView Answer on Stackoverflow
Solution 9 - C++MikolaView Answer on Stackoverflow
Solution 10 - C++thkalaView Answer on Stackoverflow
Solution 11 - C++Kerrek SBView Answer on Stackoverflow
Solution 12 - C++daveView Answer on Stackoverflow
Solution 13 - C++edgar.holleisView Answer on Stackoverflow
Solution 14 - C++ChriszumaView Answer on Stackoverflow