What are the differences between a compiler and a linker?

smruti picture smruti · Sep 30, 2010 · Viewed 32.9k times · Source

What is the difference between a compiler and a linker in C?

Answer

Stefano Borini picture Stefano Borini · Sep 30, 2010

The compiler converts code written in a human-readable programming language into a machine code representation which is understood by your processor. This step creates object files.

Once this step is done by the compiler, another step is needed to create a working executable that can be invoked and run, that is, associate the function calls (for example) that your compiled code needs to invoke in order to work. For example, your code could call sprintf, which is a routine in the C standard library. Your code has nothing that does the actual service provided by sprintf, it just reports that it must be called, but the actual code resides somewhere in the common C library. To perform this (and many others) linkages, the linker must be invoked. After linking, you obtain the actual executable that can run.