1/100 Days of C++: Introduction to Compiler, Linker, and Libraries

Hello reader, welcome to 100 Days of C++ series. In my previous post on 4 Main Reasons Why I am moving to C++ from Python, I expressed my views on how C++ programming language is more convenient for me as an automobile engineer. Not that Python is bad, I am still a fan of Python. But when you need a real-time execution, when a delay of a few hundred milliseconds is of drastic significance, C++ is more appropriate.

In this day 1 of the series, I will start with compiler, linker, and libraries in C++. Not a traditional way to start a programming language, but I don’t think we need an introduction to what a programming language is and why we need programming languages in our lives. Whereas when it comes to introduction to C++, the features and characteristics of the C++ programming language are documented in my previous blog. So if you haven’t, check it out: 4 Main Reasons I am moving to C++ from Python.

Now, I plan to learn C++ through online sources. So I thought of documenting my journey and the topics I learned. So, if you are a beginner like me and are willing to learn C++ on your own, join me on this challenging journey.

What is a Compiler?

Every programming language has its own set of rules. C++ similarly also has some protocols that need to be followed while programming such as semicolon (;) at the end of each line, curly brackets ({}) for functions and loops, etc. A compiler goes through your code and checks if your code is following these “rules”. If any, it will throw an error and stop compiling until it is corrected.

Another compiler task is to convert your code into machine language instructions. When you compile your code, it generates an object file with an extension .o.

c++

For instance, you write a Hello World code with in C++ file (.cpp extension).

#include <iostream>

int main()
{
  std::cout << "Hello World!" << std::endl;
}

The compiler will create an object file of this code which you can directly run. To generate such an object file using compiler, we execute the following:

g++ hello_world.cpp -o hello_world

Here, g++ is the compiler that will compile the code written in hello_world.cpp file. The -o argument tells the compiler into what file the output should be written. In other words, it tells the compiler what the name of the generated object file should be. If no such argument is passed, the object file with carry the same name as the source code file with the .o extension as default. The illustration below summarizes the generation of object file from the source code:

c++

So, we now understand how compilers work. They check the code for any syntax errors and help us convert the code into an object file. But what if we have multiple object files and we build an application that makes use of these object files. We need something that would help us link our application to these object files. This is where linkers kick in.

What is a Linker?

A linker, as mentioned above, helps to combine and link the required object files and produce an executable file of the desired output. So how does it work?

First, the linker reads the object files that are generated by the compiler. Next, it makes sure that the dependencies involved are defined, valid, and compatible. If you define any function, for instance, in some foo.cpp file, and call it in bar.cpp file, the linker knows the reference and will link the two files together. If for some reason the linker fails to find the reference, it leads to a linker error.

Similar to linking different .cpp files, a linker can also link a library. A library is a predefined code or collection of codes that is developed for others to build software. Take the very popular matplotlib as an example. It is a well-known library that helps to generate informative illustrations and diagrams. When you include any entity from the matplotlib library, the linker ensures that all the functions and variables, your code relies on, are available and properly defined in matplotlib.

Following me till now? Do not worry if you find this perplexing and flimsy. We will carry out an introductory exercise in C++ to understand the theory. This holds true for all of the upcoming theory in this 100 days of C++ series.

What is Building?

You might have heard the terms “build the package” or “build error” used often in development. It refers to the entire process of compiling and linking the code. If everything works fine, the build is said to be successful. If an error occurs while compiling or linking, the entire building process gets interrupted and will not be successful until the error is corrected.

Summary

In this first blog on 100 Days of C++, we learned the basics of running a program: compiling, linking, and building. Next, we’ll build our first C++ program and create an executable file that a user can run by just clicking on it.

To stay updated on this series and other interesting articles in programming and machine learning, follow me on Instagram @machinelearningsite where I post my site updates, programming tips, and sometimes memes.

Enjoyed this article? Support me by following my social media. This will motivate me to build more interesting blogs knowing that more people like the content: 🙂

Another way to stay updated is through the *free* monthly newsletter: