What does a compiler actually do?

Compiling, linking and machine code

Why compile?
The C/C++ code cannot be run as it stands in a source file. If you are familiar with Perl, PHP or ASP, this may appear strange. These languages are scripting languages rather than compiled languages. In all cases, the code must be translated into a form readable by the machine (machine code) - the difference is when this translation occurs. Scripting languages do at least some of this translation at run-time - leading to a short delay before the code is executed. Compiled languages like C and C++ do all the translation before execution. This saves time in execution and is one reason why most operating system code and application code is written in a compiled language like C.

One of the consequences of using a compiled language is that the source code cannot be easily recreated from the executable (binary) file. Scripting languages must release at least some source code for the program to be run. Compiled languages have the option of not releasing source code. Once compiled, C and C++ programs will execute on other compatible systems without the compiler being present whereas scripting languages must be distributed with the scripting engine that can complete the translation to machine code instructions for the CPU. The independence of compiled code from the compiler and the source code has contributed substantially to the current dominance of certain proprietary systems. The compiled code therefore removes freedoms from the user to benefit the provider. Free software prevents the erosion of user freedom by distributing source code, compiler and compiled executable at the same time.

Link or not to link
Compiled code can only operate without the compiler because the definitions of each keyword and function are included within the executable. When using only standard libraries, the compiler will do all the work of matching your keywords and functions with the code that actually performs the work before starting to compile the complete code. When you need to use code and libraries outside the standard library, the compiler needs the help of the linker program to match all the functions in your code. The linker uses the header files of the library files to calculate where and how to link your program to the compiled library. Different compilers and development environments configure the link in different ways. It is often done by including the library header files and location of the library itself in the project file. In GNU/Linux, this is commonly via a Makefile.



This is part of www.codehelp.co.uk Copyright © 1998-2004 Neil Williams
See the file about.html for copying conditions.