- Richard Feynman -
Compiler
A program that transforms (or transform) software written in one programming language to another programming language. It will take an entire course to give you enough information about compilers and how to write them but in our courses this will do fine.
Examples of compilers are Java Compiler which translates from Java source code to Java byte code (to be executed by the Virtual Machine) and C Compiler which translates from C source code to machine code.
Expand using link to the right to see an example of compilation of a C program.
C compilation
Let's give an example in C. Here's the source of a file called hello.c
:
#include <stdio.h>
int main(void)
{
printf("Hello world\n");
return 0;
}
Source code can be found at github: hello.c (download with curl: curl -O https://raw.githubusercontent.com/progund/junedaywiki/master/compiler/hello.c
).
To compile the file above you type the following:
$ gcc hello.c -o hello
Now we have transformed hello.c
(yet kept the original) into a program called hello
.
To execute the program (as result from compiling hello.c
you can type the following:
$ ./hello
Hello world
If we want to check the file type we can do the following:
$ file hello
hello: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 3.2.0, BuildID[sha1]=59c0391f9cb05af1e15cf6f96db55b6bf141ad8b, not stripped
Note: the printout from file
is from the author's laptop running GNU/Linux. The printout may differ on your computer.
Expand using link to the right to see an example of compilation of a Java program.
Java compilation
Let's give an example in Java. Here's the source of a file called Hello.java
:
public class Hello {
public static void main(String[] args) {
System.out.println("Hello world");
}
}
Source code can be found at github: Hello.java (download with curl: curl -O https://raw.githubusercontent.com/progund/junedaywiki/master/compiler/Hello.java
).
To compile the file above you type the following:
$ javac Hello.java
Now we have transformed Hello.java
(yet kept the original) into a file called Hello.class
.
To execute the program (as result from compiling Hello.java
you can type the following:
$ java Hello
Hello world
If we want to check the file type we can do the following:
$ file Hello.class
Hello.class: compiled Java class data, version 52.0 (Java 1.8)
Note: the printout from file
is from the author's laptop running GNU/Linux. The printout may differ on your computer.