Getting Started with C++

Linking

When writing a C++ program, the next step you have to take is to compile the program before you run it. Thus, you see the process of compilation converts the program written in a human-readable language like C++ into a machine code. This helps the Central Processing Unit to understand it directly. In other words, creating an executable file from the source file involves multiple stages. In C++, these stages include Preprocessing, Compiling and Linking.

linking

What is Linking?

Linking means creating a single executable file from several multiple object files. In this step, it is common that the linker will complain about undefined functions which are commonly main themselves.

During compilation, if the compiler cannot find the definition for a certain function, it will simply assume that the function was defined in another file. If this is not the case, then there is no way that the compiler will know of it.

It does not look at the contents of more than one file at a time. On the other hand, the linker may glance at numerous files and attempt to find references for the functions that were not mentioned.

One might wonder why there are separate compilation and linking steps. Firstly, it is probably much easier to implement things in such a manner. The compiler will do its thing and the linker will do its own.

Thus, as the functions are separate, the complexity of the program reduces greatly. Another benefit is that it enables creating large programs without redoing the compilation step every time a file changes.

Instead, it uses the so-called “conditional compilation”. Thus, it is essential to compile only those source files that have changed. As for the rest, the object files are adequate input for the linker.

Finally, it also makes it easier for implementing libraries of pre-compiled code. Simply create object files and link them just like any other object file. We refer to it as the ‘separate compilation model’ because each file compiles separately from information in other files.

In order to get a complete advantage of condition compilation, it is probably easier for getting a program to help you than to try and remember which files you have changed since you last compiled. If one is working with an integrated development environment (IDE), one may already take care of this for oneself.

Conclusion

Therefore, it is essential to know the difference between the compilation phase and the link phase. In other words, it can make hunting for bugs easier. Compiler errors are generally syntactic in nature — a missing semicolon, an extra parenthesis.

Alternatively, linking errors generally concern with missing or multiple definitions. If one gets an error that a function or variable is defined multiple times from the linker, it is a good sign that the error is that two of the source code files have the same function or variable.

Browse more Topics Under Getting Started with C++

Phases

How we compile a specific program depends on the operating system and the compiler. Nowadays, compilers are run from the IDE (Integrated Development Environment) that consists of building and analysis tools.

One simply has to click the menu for compiling the program and then executing it. However, in reality, the whole process takes place in different phases. Now, this is where compiling and linking in C++ enters.

Preprocessing

In this stage, the preprocessor will change the progrPreprocessingam as per the directives mentioned which begin with the # sign. In other words, the C++ preprocessor will take the program and deal with the # include directives. Thus, the resulting program will be a pure C++ program.

For instance, in C++ program #include<iostream> will command the preprocessor for reading all the contents of the iostream header file and include the contents into the program and produce the separate C++ program file.

Compilation

In this phase, the program will translate into a low-level assembly-level code. Moreover, the compiler will take the preprocessed file without any directives. Then, it will produce an object file that contains the assembly-level code.

The object file generated is in binary form. In the object file which generates, each line describes one low-level machine-level instruction. The assembly phase converts these object files in assembly code into machine-level instructions.

Thus, the file created is a relocatable object code. Thus, the compilation phase creates the relocatable object program which we can use in different places without compiling again.

Linking

As we have studied earlier, this phase means creating a single executable file from numerous object files. The file creates after linking is ready to be loaded into memory and executed by the system.

When we understand errors, linking and compilation are very different. The compiler shows errors in syntax. For instance, semi-colon not mentioned, data type not defined etc. However, if there is an error that function has been defined numerous times, then this error is from the linker as it denotes that two or more source code files have the same meaning and that is resulting in an error.

FAQ on Linking

Question 1: What is a linker in C++?

Answer 1: The linker refers to a program that makes executable files. It resolves linkage issues, like the use of symbols or identifiers which are defined in one translation unit and are required from other translation units. Moreover, they can take objects from a collection referred to as a library.

Question 2: What is a linking error in C++?

Answer 2: Linker Errors are errors that take place when after compilation, one links the various object files with the main’s object using the Ctrl+F9 key(RUN). These are errors created when the executable of the program cannot be produced. It can be because of the wrong function prototyping, incorrect header files, etc.

Share with friends

Customize your course in 30 seconds

Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
tutor
tutor
Ashhar Firdausi
IIT Roorkee
Biology
tutor
tutor
Dr. Nazma Shaik
VTU
Chemistry
tutor
tutor
Gaurav Tiwari
APJAKTU
Physics
Get Started

Leave a Reply

Your email address will not be published. Required fields are marked *

Download the App

Watch lectures, practise questions and take tests on the go.

Customize your course in 30 seconds

No thanks.