Updated March 28, 2023
What is Linker?
Suppose you are leading a project, and the project is divided into multiple parts on the basis of resources available and the expertise of each individual. Now, each of these small portions of the project is equivalent to a small project in itself, each having their own set of inputs and outputs. But for you to publish the entire project as a single product needs a way to combine all these pieces of the puzzle in such a way that we make a single product. In computing terms, this is what linking is, and the component which does that is the linker. So, combining all the multiple files to a single executable is what linking is. The program that takes the files generated by the compiler to make it into one executable is called a linker.
Why do we Use Linker?
Now, before we jump onto understanding the use of a linker, we need to be well versed with a few terminologies so that one will be able to appreciate the use of a linker. At first, we will look at terminology Symbols. In the source code, all the variables and functions are referred to by names. For example, suppose you declare a variable int x. In that case, we are essentially informing the compiler to set aside some memory for memory required by int. from here on, anywhere I reference “x,” I will be referring to the memory location where this variable is stored. Hence, if we mention a variable or a function, it is nothing more than a symbol to program. For us, it is easy to understand symbols, but for the processor, it means nothing more than a specific command it should execute. This command should be interpreted in machine language from the programming language a user write. For example, if we use x+=8, the compiler should convert this to something like “increase the value at the memory of “x” by 8 units”.
The next term is symbol visibility. This is more often used if 2 or more function needs to share a variable. For this, we would take the help of symbols that would have specific meaning for a programming language. For example, in C, we can use the symbol extern to reference a variable in another file, and the extern symbol will make sure to reference it to the variable in another file. Hence, increasing the visibility of the variable, which is declared only in one file.
The entire linking process is carried out in two parts; the first one is collecting all the files into a single executable file, i.e., to read all the symbol definitions and note the unresolved symbols, and then in the next step, go through each of the unresolved symbols and fix them up to right places. Post this process; there should be no unresolved symbol, or else the linker will fail. And this process of inking is known as Static Linking. This is because all these checks are performed during the compilation of the source program. An advanced concept is of dynamic linking, where variables and symbols are linked at the run time. The executable image would consist of an additional file of a sharable library.
Now let us understand the necessity of using linkers. If you are working on a huge project consisting of millions of code lines and due to customer requirements, you would have to change only a portion of a file and then compile the code again. Don’t you think, if we compile all the millions of lines of code again, it will result in unnecessary time loss. In current modern optimizing, compilers perform high code analysis and memory allocation and can take even longer. This is where linkers would come into play as most of the third-party libraries would be affected rarely, and the files which are affected by code change are less as well. Hence, when the compilation of the code is performed, the compiler creates an object file. With each file change, only the corresponding object file gets updated. Now, the job of the linker is to gather these object files and use them to generate the final executable. Here the third-party libraries are used via shared libraries.
Now, in recent times we have seen that not all OS would create a single executable. Rather it prefers to run on components that keep the functions together. For example, Windows uses DLLs for the task. This helps in reducing the size of executable, but in hand increases the dependency on the existing DLLs. DOS uses.OVL files.
Importance of Linker
In recent times, with the compiler evolving constantly, we have begun writing more optimized code every other day. Though linkers come at the very end of gluing together the object files, one would have a notion that the technology hasn’t changed much. But with recent advancements, linkers have even higher importance.
With Linker hardening, linkers will be able to remap non-dynamic sections of relocation to read-only. Having it read-only would improve things in running the code in an optimized way. Another importance is that linkers can use intermediate representation in the object files to identify sites where inlining may prove beneficial and thus help link-time optimization.
Advantages
With the discussions above, we have an idea of what advantages linkers bring in place for developers, and here we would formally put them in points.
- There need not be any duplication of the library required, as the frequently used libraries will be available in one location. An example of such an advantage is having standard system libraries.
- Change of libraries used dynamically by various other codes will easily get corrected or upgraded by the change of the library in a single space. However, the ones linked by static linking would have to manually re-link again.
- With the use of static linking, we can avoid having “DLL hell,” which is a problem when the code is working with DLLs running on a single memory space.
Conclusion
In conclusion, the crux of the entire article of linkers is that it is an inevitable part of the process of compilation of code. And with the recent developments, we have seen the level of optimization of the code has increased multi-fold. Linkers are pieces in the chessboard, which glue the different source files into a single unified program to solve a business problem!
Recommended Articles
This is a guide to What is Linker?. Here we also discuss the introduction and why we use linker? Along with importance and advantages. You may also have a look at the following articles to learn more –