Compiling and Linking in C++

lifengadget-programming

Compiling and Linking in C++

When you write a c++ program, the next step is to compile the program before running it. The compilation is the process which convert the program written in human readable language like C, C++ etc into a machine code, directly understood by the Central Processing Unit. There are many stages involved in creating a executable file from the source file. The stages include Preprocessing, Compiling and Linking in C++. This means that even if the program gets compiled, it may result in not running as errors may arise during the linking phase. Hence most IDE (Integrated Development Environment ) like Eclipse, Geany etc  consider the term build for transforming source code file to an executable file.

So following are the steps involved in building the c++ program :

lifengadget-compiling and linking in c++

Now, how you compile a certain program, that depends upon operating system and compiler. These days compilers are run from IDE (Integrated Development Environment) that contains building and analysis tools. You just have to click the menu to compile the program and then execute it. But actually the whole process occurs in different phases. That is where compiling and linking in c++ comes.

Preprocessing

 

In this phase the preprocessor changes the program according to the directives mentioned (that starts with # sign). The C++ preprocessor takes the program and deals with the # include directives and the resulting program is pure c++ program. For example, in c++ program #include<iostream> will tell the preprocessor to read all the contents of the iostream header file and include the contents into the program and generate the separate C++ program file. C++ supports many preprocessor directives like #include, #define, #if, #else etc

 

Compilation

 

This phase translates the program into a low level assembly level code. The compiler takes the preprocessed file ( without any directives) and generates an object file containing assembly level code. Now, the object file created is in the binary form. In the object file created, each line describes one low level machine level instruction . The conversion to assembly language is important as it is common output language for many compilers of different high-level languages.

 

There is also assembly phase which converts these object files in assembly code into machine level instructions and the file created is a relocatable object code. Hence, the compilation phase generates the relocatable object program and this program can be used in different places without have to compile again.

 

But you still can’t run these object files until to convert them into executable file, now here linker comes into play, which links all the object files to generate single executable file.

 

Linking

 

Linking as the name suggests, refers to creation of a single executable file from multiple object files. The file created after linking is ready to be loaded into memory and executed by the system . There is difference in linking and compilation when it comes to understanding errors. Compiler shows errors in syntax, for example semi-colon not mentioned, data type not defined etc but if there is an error that function has been defined multiple times, then this error is from linker as its indicating that two or more source code files have the same meaning and that is leading to an error.lifengadget-compiling and linking in c++

 

 

    • The source file refers to program files that we want to compile and execute. Whether we are using IDE or command line interface, the program to compile will get stored in source file.

 

  • Source file in C++ or any other programming language consists of two parts: file name — for example, factorial and file suffix — for example, in c++ it is .cppSuffix depends upon which compiler a program is being run — for example, .cpp, .cxx, .cp etc.

 

 

  • Using command line interface, there are different commands to invoke the compilers. For GNU compiler, the command to invoke the compiler is g++:

 

 

 

                                      $ g++ factorial.cpp -o facto

    • The above command will compile the program and will generate the executable file. $ sign represents the command prompt, g++ invokes the GNU compiler, factorial.cpp is name of the source file to compile and -o acts as an argument which name the result executable file, here after compilation file name facto will be generated. echo command can be used to check number of errors.

 

  • For Microsoft compilers, the command to invoke the compiler is cl :

 

 

 

                                               c:\directory location> cl -GX factorial.cpp

              This command will compile the program and will generate the executable file Here is an another  way of looking at the whole process :

lifengadget-compiling and linking in c++

 

 Errors in different phases

 

Now, lets discuss various errors that may occur in compiling and linking in c++. The errors can be of any type may be Preprocessing errors, Compiling errors, linking errors or run time errors.

Preprocessing Errors

The errors in this phase may arise due to wrong spelling of the header file or the location is not specified properly.There can be other reasons as well, for example sometimes #define statement may lead to errors as follows:

#include<iostream>

#define pi 3.14 // header file statement

Now in above program, the pi value will get replaced by 3.14//header file statement and this will lead to an error.

Compiler Errors

These errors arise mostly due to syntax not met or some undefined symbol used. For example :

#include<iostream> // header file

using namespace std;

int main()       
{

int a=10;     //floating point variables

cout<<"Before interchange the value is"<<a<<" "<<b<<endl;       // display message onto the screen

a=a+b;  //undefined symbol b
b=a-b;
a=a-b    //error as no semi-colon

cout<<"After interchange the values are "<<a<<" "<<b;

return0;  //undefined symbol return0
}

The  above mentioned program has many compile errors, like semi colon not mentioned, it should be return 0 rather then return0.

Linking Errors

The linker checks for an errors of missing symbol references in the object code generated by compiler. For example, if there is a reference to certain header file and that file is missing then the linker will generate an error “unresolved external reference cout<<” ,  if there is no iostream header file mentioned.

#include<iostream>

int mains()  //main() function missing
{

int a, b;
cout<<"hello world"<<endl;
return 0;

}

Now here linker will generate an error, as missing main() function, as execution of the program begins with main(). Hence these were the certain concepts which explains how Compiling and Linking in C++ works

[poll id=’22’]

You Might Also Like

  • https://plus.google.com/+ArtemPelenitsyn Artem Pelenitsyn

    Why there’s no indentation in listings?

    • sunil

      it think the main focus was on concept… listings are just for the sake of examples

  • http://www.bernaridho.net Bernaridho

    The term object code is bad, no matter how common it is. The .o or .obj is not machine code. Do we compile file? I don’t think it’s a correct notion.

    • Aloha

      So what is the correct notion then!! .obj can represent machine code!

  • William

    “For Microsoft compilers, the command to invoke the compiler is c1 ”
    Actually, it’s cl

    • rustyocean

      yaa!!! that was typing mistake!! thank you for noticing!!

  • Rusty Programmer

    Good article!! add comparison of iterative and recursive procedures also!