Published by
Dec 6, 2013

Compiling and Linking in C++

Score: 3.5/5 (198 votes)
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.

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.


In this phase the preprocessor changes the progrPreprocessingam 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


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

Attachments: [Selection_101.png] [Selection_103.png]

  • 1793521592 2018-02-23
  • 2864591591 2018-02-23
  • 6167231590 2018-02-23
  • 3669201589 2018-02-23
  • 7946381588 2018-02-23
  • 8957701587 2018-02-23
  • 3891941586 2018-02-23
  • 6039851585 2018-02-23
  • 2573991584 2018-02-23
  • 7728781583 2018-02-23
  • 3731582 2018-02-23
  • 1007451581 2018-02-22
  • 8908121580 2018-02-22
  • 141161579 2018-02-22
  • 9421578 2018-02-22
  • 2826901577 2018-02-22
  • 3647361576 2018-02-22
  • 5717551575 2018-02-22
  • 523811574 2018-02-22
  • 6439871573 2018-02-22