This is the third installment in a series explaining the common components of software development environments. To get the sequence, make sure to check out part 1 and part 2 of the series, in which text editors and interpreters are covered.
Today, we’ll talk about another component in the toolchain.
In a way similar to the interpreter, the compiler needs to know about the language you’re working on.
The compiler reads a file in a particular format and outputs a file in another one; this can be thought of as a translation. Compilers usually translates source code to machine code.
The part of the compiler that generates output from the source code – as opposed to executing it as the interpreter does – means that the compiler needs to understand how to write a file that can be executed in a particular environment.
It is common for compilation in a particular environment to produce a file that can be executed in that same environment, but not necessary. When a compiler produces a file that can be executed in another environment, the activity is called “cross-compiling”, to signal that the file is aimed towards a different environment.
In short: A compiler is a program which knows about the programming language you are using and the environment in which it the resulting program is supposed to be executed.
As is the case with interpreters, there can be more than one compiler for any particular language and targeting any particular environment. Here’s a list of some gratis compilers:
- gcc; this is a widely used compiler collection. It’s a program which can compile from several programming languages and into several platforms. Some of the languages it works with are: C, C++, Objective-C and FORTRAN. It can produce executable for many target environments as well.
- clang; this is a compiler aimed at the C family of programming languages, and can work with C, C++, Objective-C and Objective C++; it supports several target environments, notable x86, x64 and ARM processor architectures, which most computers and cellphones use.
- fpc; this compiler is aimed towards the Pascal language and its variants, including Object Pascal. It supports many environments, so much that the “write one, compile anywhere” philosophy is a core idea for the compiler.
- javac; this compiler is aimed towards the Java programming language, and targets the Java Virtual Machine environment. It’s notable as part of the category of compilers that don’t have a physical machine as a target, but a virtual machine which can then be written in another language and compiled for any desired target. Thus, writing and compiling once would allow a Java program to run anywhere a Java Virtual Machine was compiled to run beforehand.
There are some caveats I’d like to make at this point:
- Execution environments are commonly called platforms, and are usually the combination of a particular processor and a particular operating system.
- I stated before that the output is usually an executable file. This is not precise information, merely a simplification. The full explanation is that the compiler usually outputs an object code file. It so happens that object code is the code that can get executed by the computer, but it isn’t necessarily formatted in a way that the operating system executes it directly. When not producing executable object code, the compiler is usually producing referable object code.
- I said a compiler usually produces object code; but because of the nature of the compiler, taking from a source language and producing some executable output file, when a program produces “bytecode” (object code for virtual machines) or it produces a source file for an interpreter to use (in which case it can be called a transpiler), it’s still a compiler.
Now, object files don’t usually have all the machine instructions that need to be executed in order for the program to function; why this happens and how this fact fits with the successful production of executable programs will be explored Tomorrow, when we cover the linkers. The answer is related to the non-executable object code files that the compiler can produce.
This is all for Today. I hope this was entertaining and informative.