Folder101   Home Notes Quiz Assigns Info  
Forum Forum
101 Home 101
 
  C++ Level 2

Compiling, Linking and Executing

Introduction

Compiling Source Code

Linking Object Code

Summary of Steps in Creating a Program

Summary of File Types

Extension Work


  Introduction

Computers are 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:-

00401277 mov ecx,dword ptr [this]

People find it difficult to learn machine code and prefer to write code using a high-level language like C++. A line of code such as:-

cout << "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.


  Compiling Source 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.


  Linking Object Code

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 is:-

iostream.h

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.


  Summary of Steps in Creating Your Program

  1. Create your source code.

    Type your code in and save the file. It should have a .cpp extension.

  2. Compile your source code.

    This creates object code. A file will be produced with a .obj extension.

  3. 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.

  4. Run your program

    The executable can now be run independently of the VC++ development environment.


  Summary of File Types

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

PROJNAME.DSW

This is the workspace file used within the development environment. It organizes all the projects into a single workspace.

PROJNAME.OPT

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.

PROJNAME.DSP

This is the project file used within the development environment. There will be a separate .DSP file for each project you create.

PROJNAME.H

This is the main include file for the program. It contains all global symbols and #include directives for other header files.

PROJNAME.CPP

This file is the main program source file.

PROJNAME.OBJ

This file is the compiled code.

PROJNAME.EXE

This file is your completed program. It will run independently of VC++


  Extension Work

Carry out the following exercise:-

  1. 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.




Thats it!
Now try the quiz questions.