linkers

“A chain is only as strong as its weakest link”

In this section we will explain what “linkers” are and how they get involve in the process of compiling an application and make it ready to be run by the target computer.

Let’s take a look at a simple C-language code:

void hello(void) {
printf("Hello, World!n");

}

Let’s say somehow a computer managed to run this code. The result is that you’ll see a message on the display device which says Hello, World!.

The printf in this example is called function and its job is to send any message we put within the parentheses to the display device (monitor).

Note: printf() is a C-language function.

But how this operation is done via just calling one simple function? What and where is the instructions of the printf() function?

The fact is that this function is connected to other pre-written source-code that does all of those connections and basically heavy works behind the scene so that our sentence sent to the monitor.

You can think of this printf function as an interface to another service behind the scene that does the operation of sending your message to the output.

Within any programming language there are many functions and pre-written services like printf function that you just saw and any developer can access and use them for their advantages.

Together these services and functions are called library.

Because there are many functions and services available to use and not all applications out there need all these services, they are stored and organized in different files and we can import any of them in our source code when we need them.

Note: Even you can write your own services and interfaces (your own library) and call them within another source-code.

But the important note is that when our application is running on the target machine, the main source-code of any services that we called in our own source-code (like printf() function) should be already assembled in.

How?

This is where linkers come in.

Linkers are basically a program that comes after our source-code is compiled and they will search through our compiled source-code in order to find all those external services and functions that we used within the application. After the linker found the main source-code of each of the service in the application, it will attach them to the compiled program and create the final executable application that can be ran on the target computer.

Linkers also attach other codes named “startup-code” to our application. This startup-code runs before the application is actually run on the target computer and its job is to prepare the space and any other housekeeping work that needs to be done before the application runs on the target machine.

At the end we should know that all of these operations that a linker does, is automatically done behind the scene and you won’t even see it. All we as a developer do is to call the related compiler of our chosen language and the compiler will do the rest of heavy works for us (compiling and linking all the source-codes) and finally it will give us the executable file that we can simply run and start to use.