Converting a C++ source file into an executable


To enable students to:

  1. describe source, object and executable code

  2. create source code

  3. understand library files

  4. produce an executable program

Describe source, object and executable code

Computers consist of millions of electronic gates that may be either switched on or off. In the original machines operators would program computers by throwing switches on their front panel to turn gates on or off - a laborious task. To ease the task programs were written to automate the process, reading instructions from paper tape or cards rather than physical switches. After many years of evolution these instructions have mutated from the extremely cryptic, such as 01100010, through the hard, for example LDA 0x001A, to the merely tricky like for(long x = 0; x < 100; x++);, while the media has progressed to text files stored on disks.

Source code consists of instructions written in a text editor using a well defined and syntactically strict but human readable language. Although intelligible to people it is gobbledegook to computers.

In order for a program written by a human (i.e. the source code) to be understood by a computer it must be converted to executable code via a process called compilation. This process translates instructions such as for(long x = 0; x < 100; x++); to many hundreds of binary instructions (e.g. 01100010110010100011010111110000...)

Large programs consist of many hundreds of source files. If they had to all be re-compiled whenever a change took place development would be a very slow process. Instead compilers produce intermediary files (termed object code) - one object file for every source file which are linked to produce executable code. If a source file changes the compiler will only translate that one file, producing a new version of the corresponding object file. By linking the new object file with the existing ones a new executable is obtained.

Neither executable or object code is intelligible to humans, and so although it may be loaded into an editor (as can all files) it is essentially un-editable.

Create source code

Generation of the source code is developer dependent, individuals have their own favourite editors (for example on DOS some may use the EDIT program).

Programs may consist of many source files, consequently they must all have meaningful names which help other developers to understand what purpose that file serves.

It is mandatory for all C++ source files to have the extension .cpp which indicates to the compiler that it is dealing with a C++ source file.

To aid developers the intermediary object files produced by the compiler have the same file name as the source file but a different extension, this time .obj

The executable code that is eventually produced by linking all the object files together is written to a file with a .exe extension.

Understand library files

Many tasks that developers wish to perform have already been done by others. To save effort for programmers the compiler writers have written may useful standard functions, for example displaying text on a screen, obtaining user input from a keyboard, sorting text into alphanumeric order, etc.

To ensure consistent behaviour, these functions should never be touched by application developers - imagine the chaos that would ensue if a developer decided to change the order of a sort routine to match his programs needs while another developer was relying on its previous behaviour. To enforce this the functions are not provided in the form of source code. Instead collections of them are grouped together in special forms of object code termed libraries and provided free when the compiler is purchased.

The compilers linking stage has already been introduced as the point when all the object files present in a project are combined together to generate executable code. This stage is also responsible for linking in any libraries required by the source code.

Produce an executable program

There are many different compilers, each having their own sets of commands to compile source files and link object files to produce executable files, but essentially they all follow the same set of operations.

First the pre-processor will work through the source code, making appropriate modifications. Once complete the compiler will convert the source into object code. The linker will then step in to combine all the object files and libraries together into an executable program. Depending on options chosen during the compilation and link stage the resultant program will either be stand alone or reliant on the libraries used being available at run-time. Stand alone programs contain copies of all the libraries used within them, so although being more transportable than the library dependent types they will also be much larger. Note, a few compilers exist that do not permit the generation of stand alone programs - the executable code they generate always needs the support of run time libraries.

Programmers will often make mistakes during the development process. If they do not stick to the rules of the language they will encounter compilation errors from the compiler and no executable program will be produced - the errors in the source code must be corrected before compilation can be completed. However, if they do not think the logic of their program through sufficiently they will encounter run time errors, for example a calculator program may try and divide two numbers provided by a user, if the divisor is zero then the program will fail with a division by zero run time error - the programmer should have anticipated this error and ensured the divisor was not zero.