Compiling, Linking and Executing
Summary of Steps
in Creating a Program
of File Types
only capable of recognizing and executing commands given in their own
native processor instruction set. This is called 'machine code.'
Here is an example of machine code:-
mov ecx,dword ptr [this]
it difficult to learn machine code and prefer to write code using a
high-level language like C++. A line of code such as:-
<< "Hello World!\n";
... makes more
sense to us than that line of machine code- don't you think?
A high level
language is fairly easy to understand once you are familiar with the
language. However, a computer doesn't understand this type of code and
so it has to be converted into machine code.
This is the job of a compiler
- it converts high-level code into machine code.
For our convenience we can write source
code in a high level language and then compile it into machine code.
We can type source code into any text editor or use an IDE like VC++
and save it as a normal text file.
By convention, C++ source code is
saved in a file with a .cpp extension. This source
code file is not a program however and it can't be executed (run) as
a program can. To make your program run first you have to compile
your source code into object code.
When this source code has been compiled,
another files is created by the compiler with a .obj
extension. This contains the object code for your program. However,
you still cannot run your program using the .obj file.
You need to create an executable file.
This is the job of the linker
- it makes an executable out of your .obj file.
The job of a linker is to links your
object code with any other object files and/or library files that may
be necessary to make a stand-alone executable file.
An example of a library file that may need to be linked to your program
In the HelloWorld program,
you need to link to the iostream
library to be able to use the cout
function and send output to the command prompt console. In VC++ you
can invoke the linker by selecting 'Build' from the menu.
This process creates an executable file with the extension .exe.
Your program can now be run using the exe file.
- Create your source code.
Type your code in and save the file. It should
have a .cpp extension.
- Compile your source code.
This creates object code. A file will be produced
with a .obj extension.
- Run the Linker
This process links your object file with other
object files and libraries. An executable file is produced with a
.exe extension. In VC++ you select 'Build'
from the menu.
- Run your program
The executable can now be run independently
of the VC++ development environment.
As we have seen creating a program
involves a variety of different types of files. Here is a summary of
some of the file types you will come across when creating a VC++ program
This is the workspace file used within
the development environment. It organizes all the projects into a single
This is the workspace options file
used within the development environment. It stores all the user options
you create for your workspace, so that each time you open the project
workspace it has the look and feel you want and includes any customizations
you have made.
This is the project file used within
the development environment. There will be a separate .DSP file for
each project you create.
This is the main include file for
the program. It contains all global symbols and #include directives
for other header files.
This file is the main program source file.
This file is the compiled code.
This file is your completed program. It will
run independently of VC++
Carry out the following exercise:-
- So far you have been using commands on the Build
menu to compile, build and execute your code. There is a quicker way.
You can use the toolbar icons instead.
Don't click any of the icons yet. Hover your mouse
over each icon until you locate the Compile icon. Click
on it to compile your code. Now hover the mouse over the remaining
icons until you locate the Build icon. Click on it to
build your code. Then find the Execute button and click
on that. Your program should run then.
Now try the quiz questions.