-6

What language is a c or c++ header file written in. Another big doubt that I have is, since a computer understands only binary, how do people actually write a programming language and a compiler that the computer can actually understand?

ketan
  • 19,129
  • 42
  • 60
  • 98
VisnuGanth
  • 71
  • 1
  • 9
  • 1
    C header files are written in C (the same for C++). The [C Standard](http://port70.net/~nsz/c/c11/n1570.html) describes the preprocessor just as well as it describes the rest of the language. – pmg Dec 20 '15 at 17:43
  • The second part of the question is an interesting problem (bootstrapping a compiler), but the first part is not clear, what do you mean ? A c/c++ header is written in c/++ by definition. – Ilya Dec 20 '15 at 17:45
  • Yes, I just realized that the first part is dumb but can you elaborate your answer on the second part of my question – VisnuGanth Dec 20 '15 at 17:47
  • @VisnuGanth: I think I have explained roughly how this process works these days and in the past. – Mats Petersson Dec 20 '15 at 17:54
  • You may want to check out http://programmers.stackexchange.com/questions/84278/how-do-i-create-my-own-programming-language-and-a-compiler-for-it and/or http://stackoverflow.com/questions/1669/learning-to-write-a-compiler – dat Dec 20 '15 at 17:56
  • What language is a python program written in?? C'mon. – Deanie Dec 21 '15 at 03:19

1 Answers1

3

The C and C++ header files are written as C and C++ source code respectively. The code in these [1] is compiled and linked into your executable file.

A compiler is written in some arbitrary language - these days, you typically take an existing C or C++ compiler for some platform that you have available, but ages ago, the process was basically to write a very basic compiler in assembler [or some other available and suitable language], and then use that to bootstrap into a higher language compiler.

Of course, if you have just invented "Chip X", and haven't got a portable assembler, you'd also have to write an assembler. Hopefully you have some OTHER computer with a programming language - but if we pretend that no computers are available, then we'd have to come up with binary code "by hand", and then enter that into the ROM of the computer. That code would perhaps be able to perform some really simple task such as printing "Hello" to some output device. Once that works, we'd expand it to have a loader, so we can load a binary file (or add new commands some other way). A very simple editor to edit files, and a file-storage would be very useful to have. And then we could start writing some code that can read human readable instructions (assembler code) and produce binary from that. Once we have an assembler, we can write a program in assembler that takes (very simple) C input and outputs assembler. Assemble that code, and we have a (very simple) C compiler. Now we can use the simple C compiler to write a better C compiler in "simple C". Keep at this for a while, and you end up with a decent C compiler... But it's probably a few years worth of work unless you have done this sort of thing many times...

Any language that can read text files and compare strings and output binary files in "free format" is pretty much usable to write a compiler. It's of course not trivial.

I have written a compiler for Pascal which uses the LLVM compiler framework to produce the actual code meaning, I've done the simple part of the compiler, the hard part in a good quality compiler is the code-generation pass, and I only do that into LLVM Intermediate Representation, which is the whole idea of LLVM - it's a simplified machine code "language", and then LLVM provides IR -> machine code for your language. My compiler is currently about 13400 lines of C++ code - the code generation and optimisations in LLVM is millions of lines - much of which I don't even know how it works [beyond the simple overview what the function does according to the description]

[1] There are typically also libraries, which contain larger functions that aren't suitable to store in the headers directly. These are built using the same compiler [or one compatible to it] that you use to build your source into a binary file.

Mats Petersson
  • 126,704
  • 14
  • 140
  • 227