anand
anand

Reputation: 1485

Difference between static linking and dynamic linking

What is the difference between static linking and dynamic linking?

Upvotes: 16

Views: 39902

Answers (5)

Linuxdev
Linuxdev

Reputation: 11

If we compile simple helloworld program with static and dynamic linking, we will easily see the size difference as below,

#include <stdio.h>
int main(int argc, char **argv) {
    printf("Hello World !");
    return 0;
}

Create a dynamic linked executable as,

 $ gcc -o helloworld helloworld.c 

Also, create a statically linked executable as,

 $ gcc -o helloworld_static helloworld.c -static 
 $ ls -alh helloworld
-rwxrwxr-x 1 myuser myuser 7.2K Sep  2 21:08 helloworld 
$ size helloworld
   text    data     bss     dec     hex filename
   1124     276       4    1404     57c helloworld
$ ls -alh helloworld_static 
-rwxrwxr-x 1 myuser myuser 712K Sep  2 21:08 helloworld_static
$ size helloworld_static    text      data     bss     dec     hex filename
 658287    4096    3644  666027   a29ab helloworld_static

hence, the observations of executing simple helloworld program are as below,

  • A statically linked executable requires more space compared to a dynamically linked executable. The is a disadvantage of static linking and advantage of dynamic linking.

  • Only one copy of a shared library resides in memory for dynamically-linked executables. If several processes call the same object module of a shared library simultaneously, they all use the same copy of the library. Whereas, for static linking, all used objects of a library are copied into each executable. Thus, copies of each library object reside in memory for each of the different processes. This results in more RAM used for the static-linked executables than dynamically-linked executables.

  • Dynamic linking has more system calls, which means dynamically-linked executables will need more time for running/execution.

If you want a more extensive explanation, with actual system call tracing using strace during the program execution, you can visit a page I wrote about this issue on my website.

Upvotes: 0

Akanksha Tiwari
Akanksha Tiwari

Reputation: 21

static linking increase the file size of your program and it may increase the code size in memory if other applications are running on the system... on the other hand dynamic linked program take up less space and less virtual memory

Upvotes: 2

Maheswar reddy
Maheswar reddy

Reputation: 11

In static linking libraries linked at compile time, but code size is more when you this static linking ,when you only one or two programs then you use static linking

In dynamic linking libraries linked at run time(or) execution time ,but code size is less,when you have more programs then use dynamic linking.

Upvotes: 1

Dexter
Dexter

Reputation: 2480

In static linking, functions and variables which are defined in external library files are linked inside your executable. That means that the code is actually linked against your code when compiling/linking.

With dynamic linking external functions that you use in your software are not linked against your executable. Instead they reside in a external library files which are only referenced by your software. Ie: the compiler/linker instructs the software on where to find the used functions.

On windows platforms you can even explicitly load DLL files at run time and hook up the functions contained in the DLL.

Upvotes: 15

Will Dean
Will Dean

Reputation: 39530

Static linking is done at 'compile time' by a tool called a linker. Dynamic linking is done at run time, by the operating system.

Upvotes: 12

Related Questions