Skip to main content
Engineering LibreTexts

1.7: Linking and Libraries

  • Page ID
    29011
  • Now We Link

    The linker is a program in a system which helps to link a object modules of program into a single object file. It performs the process of linking. Linker are also called link editors. Linking is process of collecting and maintaining piece of code and data into a single file. Linker also link a particular module into system library. It takes object modules from assembler as input and forms an executable file as output for loader.

     

    COMPILER_STRUCTURE_EXPLANATION_-_Page_1.jpg
    Figure \(\PageIndex{1}\): Compiler Structure Explanation. ("COMPILER STRUCTURE EXPLANATION" by Sharmila chandrakanthWikiMedia is licensed under CC BY-SA 4.0)

    There are two types of linking

    1. Static Linking –
    Linking is performed at both compile time, when the source code is translated into machine code and load time, when the program is loaded into memory by the loader. Linking is performed at the last step in compiling a program.

    • Symbol resolution – It associates each symbol reference with exactly one symbol definition .Every symbol have predefined task.
    • Relocation – It relocate code and data section and modify symbol references to the relocated memory location.

    The linker copy all library routines used in the program into executable image. As a result, it require more memory space. As it does not require the presence of library on the system when it is run . so, it is faster and more portable. No failure chance and less error chance.

    2. Dynamic linking – Dynamic linking is performed at run time. This linking is accomplished by placing the name of a shareable library in the executable image. There is more chances of error and failure chances. It require less memory space as multiple program can share a single copy of the library.

    Here we can perform code sharing. it means we are using a same object a number of times in the program. Instead of linking same object again and again into the library, each module share information of a object with other module having same object. The shared library needed in the linking is stored in virtual memory to save RAM. In this linking we can also relocate the code for the smooth running of code but all the code is not relocatable.It fixes the address at run time.

    The final executable file output by the linker (see dieagram to the left) will consist of both our compiled program and the standard library if the linking was static. On the other hand, if the linking is dynamic, the linker marks the symbols that need to be found at runtime. 

    When we run the program, the loader will load the library that was dynamically linked to our program. It loads the contents of the standard library into the memory and then resolves the actual location of any function calls that are necessary. The same library that is now loaded into memory can also be accessed by other programs that need to resolve their function calls.

     

     

     

    Adapted from: 
    "Intermediate Code Generation in Compiler Design" by Aaqib Bashir is licensed under CC BY-SA 4.0

    • Was this article helpful?