What are the differences between a compiler and a linker?
Asked Answered
R

3

54

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

Rosenblum answered 30/9, 2010 at 14:5 Comment(0)
S
95

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.

Sallust answered 30/9, 2010 at 14:8 Comment(1)
+1 good explanation. Note that on most OS, there are actually two linkers involved: The linker that runs after compilation and produces the executable file (usually just called "linker"), and the linker that links in dynamically loaded libraries (aka .so or .dll) at execution time (usually called "dynamic linker").Irradiate
C
30

A compiler generates object code files (machine language) from source code.

A linker combines these object code files into an executable.

Many IDEs invoke them in succession, so you never actually see the linker at work. Some languages/compilers do not have a distinct linker and linking is done by the compiler as part of its work.

Canal answered 30/9, 2010 at 14:10 Comment(0)
A
1

In Simple words -> Linker comes into act whenever a '.obj' file needs to be linked with its library functions as compiler doesn't understand what is (scanf or printf..etc) , compiler just converts '.c' file to '.obj' file if there's no error without understanding library functions we used. So To make 'obj' file to 'exe'(executable file) we need linker because it makes compiler understand of library functions.

Argal answered 9/9, 2021 at 8:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.