66

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?

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
Jeff
  • 14,365
  • 8
  • 30
  • 30
  • 1
    "compile into C/C++"??? C++ used to compiled in C. Perhaps you might want to do a little reading and clarify this a bit. – S.Lott Jun 27 '11 at 20:57
  • 7
    Duplicate of http://stackoverflow.com/questions/1721804/which-general-purpose-programming-languages-implementations-compile-to-c – bdonlan Jun 27 '11 at 20:59
  • 7
    Any language could be compiled into C or C++. So I'll assume you're not actually asking about *languages* themselves, but about which languages have *specific implementations* that compile into C or C++. – Brennan Vincent Jun 27 '11 at 20:59
  • 1
    What you're looking for is a "translator". – Mateen Ulhaq Jun 27 '11 at 20:59
  • 3
    I would argue that it's still a compiler unless there's a close correspondence between the input code and output code (to the point where it's basically just a quick AST transformation). If your 'translator's doing type propagation, etc, then it's probably closer to a compiler. – bdonlan Jun 27 '11 at 21:00
  • 7
    Interesting question. Languages in their early stage of development usually compile to C as a precursor to full language development, in an attempt to validate that language features work as expected. Once this is done a language specific front-end is developed as a plugin to an existing back-end. But I would be curious about languages that are specifically designed to just to make using C++ easier. Thus the re-open vote. – Martin York Jun 27 '11 at 21:15
  • 1
    @muntoo: This is still a compiler. It is just one that uses C/C++ as the intermediate language. If you look at g++ for example it generates output at several levels but it is not referred to as a translator. – Martin York Jun 27 '11 at 21:17
  • 2
    Exact duplicate (or near enough) of http://stackoverflow.com/questions/1721804 – Norman Ramsey Jun 28 '11 at 05:01
  • 1
    Unfortunately, this question is not a good type of question for Stack Overflow, so once the bounty has been awarded I will close it as "not-a-real-question". The reasoning behind this is that you're basically asking people to list all such languages, and then *vote on which language is best* (or whatever voting criteria exists for such a list.) There is no single correct answer, and thus this question is not appropriate for Stack Overflow. And before anyone asks, no, it won't be migrated, it's not a good question on *any* of the SE-sites. – Lasse V. Karlsen Jul 12 '11 at 23:40
  • GCC [MELT](http://gcc-melt.org/) is a domain specific language (to extend GCC) translated to C/C++ ... – Basile Starynkevitch Jun 14 '13 at 20:08
  • Nim can compile to C, C++, and Javascript: https://nim-lang.org/ (The C code it produces is efficient and is said to be pretty readable.) – dgo.a Sep 05 '17 at 03:51

14 Answers14

19

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

Mark Knol
  • 9,663
  • 3
  • 29
  • 44
M.Stoffregen
  • 221
  • 1
  • 4
  • A List of Go-to-C, & Hexe-to-*, *-to-Haxe, & other (source-to-source) Transpilers / Translators / Transformers / TransCompilers : https://stackoverflow.com/questions/64180191/ – atErik Oct 18 '20 at 03:56
  • A List of Go-to-C , & Go-to-C++/Cpp , & Hexe-to-* , & *-to-Haxe , & other source-to-source Transpilers / TransCompilers : https://aterik.github.io/Transpiler.and.similar.List/ – atErik Oct 24 '20 at 14:50
15

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.

Sean
  • 5,290
  • 2
  • 24
  • 21
10

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.

stusmith
  • 14,003
  • 7
  • 56
  • 89
10

OOC is very new but quite nice.

tsg
  • 2,007
  • 13
  • 12
9

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

GWW
  • 43,129
  • 11
  • 115
  • 108
8

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.

Vinicius Kamakura
  • 7,665
  • 1
  • 29
  • 43
4

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.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
4

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

sigjuice
  • 28,661
  • 12
  • 68
  • 93
3

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/

Mikola
  • 9,176
  • 2
  • 34
  • 41
  • 3
    The "why" is actually easy to explain: you could (i.e. UE4, as I am now) want to use an framework, where only C/C++ is accepted, but you want to use a higher-level language. Cross-platform requirements of the framework might preclude simply using some other language, and *linking* to the framework. – Sebastien Diot Jun 08 '17 at 10:05
  • Another reason so you would have higher-level debugging for example C++ presuming you haven’t yet developed a debugger for your programming language. – Shelby Moore III Dec 28 '18 at 06:25
1

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...

thkala
  • 84,049
  • 23
  • 157
  • 201
0

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

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
0

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

dave
  • 1,520
  • 11
  • 8
0

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

edgar.holleis
  • 4,803
  • 2
  • 23
  • 27
-2

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.

Chriszuma
  • 4,464
  • 22
  • 19
  • 2
    -1 : "*Python is always a good choice, and it can do everything that C/C++ can*" ...This is balant misinformation ; nothing could be further from the truth. – An Ant Mar 26 '21 at 15:56
  • @AnAnt But this is at least partly true: there are several compilers that [translate Python to C or C++](https://stackoverflow.com/questions/4650243/convert-python-program-to-c-c-code). – Anderson Green Jun 23 '21 at 19:08