Home » Compilation, Linking and Loading in C

Compilation, Linking and Loading in C

Compilation, as you likely know, is the programmatic process of taking source code (usually text, i.e. a JavaScript or C++ file), and transforming it into an executable unit of code independent of the original source. That is to say, when you run a program written in C, you’re not running C code, but instead running compiled machine code.

Linking is the process of taking several units of compiled code that would otherwise not be executable alone, and combining them into one unit that can be run.
Also, Linking is the process of taking some smaller executable and joining them together as a single larger executable.
Types of Linking:
1) Static linking
2) Dynamic linking.
Static describes something done before program execution. Static linking occurs at compilation time, hence it occurs prior to loading a program. With static linking the external symbols that are used by your program (e.g. function names) are resolved at compile time.Dynamic describes something done during program execution.

Dynamic linking occurs at run time, so it occurs after or at the time of the loading of a program. With dynamic linking the symbols are resolved either at loading time, or at run time when the symbol is accessed (lazy binding).

A Linker must be able to
* produces an executable file from several object files.
* relocates separately compiled code segments.
* resolves external references.

Loading is part of linking – it’s how to load object files in memory and analyze them as units of data that can eventually be combined into an executable.
Loading is loading the executable into memory prior to execution.

A loader must be able to
* set up text and initialized data in memory
* initialize register copies in the process control block
* initialize the PC copy in the process control block

Dynamic linking and loading has three important benefits:

  • Software always uses latest versions of shared libraries.
  • Executable files are smaller. They do not include the shared libraries.
  • The total memory footprint for multiple processes is reduced. With virtual memory, different programs using the same library function only need a single copy in physical memory. If designed carefully the shared library subprograms can have different logical addresses in different programs.

Leave a comment

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

‘C’ Practical

  1. To get fomiliar with the structure of a ‘C’ program and print “Hello World!” on computer screen.
  2. To perform addition, subtraction, multiplication and division operations on two integers.
  3. To use unformatted 1/0 functions (getchar() and putchar(), gets() and puts()).
  4. To use scanf() function to read integers (%d, %i, %o, %u, %x), characters (%c), floating point numbers (%f, %g, %e), strings (%s, scanset).
  5. To use printf() function to format and print output (%d, %i, %o, %u, %x, %e, %E, %f, %g, %c, %p, %c, %s, %n)
  6. To change width, precision and alignment of the output of printf().
  7. To perform and, or, not, exor, left-shift and right-shift operations on integers.
  8. To check whether a given integer is even or odd (if … else statement).
  9. To write month name corresponding to a month number (switch … case).
  10. To check whether a given integer is prime using while loop.
  11. To reverse the digits of a given positive integer using while loop.
  12. To generate the multiplication table of a given integer using for loop.
  13. To generate first n terms of Fibonacci series using do-while loop.
  14. To compute factorial of a given integer using for loop.
  15. To compute sum of elements of a one dimensional integer array.
  16. To find the largest element in a one dimensional array.
  17. To perform matrix addition and matrix multiplication using 2-D arrays.
  18. To convert the alphabetic characters of a string to uppercase.
  19. To find the length of a string.
  20. To use Standard Library String Functions (strcat(), strrev(), strcp(), strcmp())
  21. Writing a function to find sum of two integers.
  22. Writing a function to swap value of two integers (call by reference).
  23. Writing a function to compute factorial of a given integer (using recursion).
  24. To read and print multiple data items (roll_no, name, branch, semester etc.) pertaining to an individual student.
  25. Demonstrating the use of union
  26. Demonstrating the use of address and dereferencing operators
  27. Performing pointer arithmetic to manipulate an array
  28. To read the contents from a file and display it on screen
  29. To read names and addresss of persons and write them into a file.
  30. Write a program to read the contents of two different files and write it into the third file.