16

What is the difference between static linking and dynamic linking?

anand
  • 1,375
  • 5
  • 14
  • 13
  • @Andrey: This is not an exact duplicate. There are really three possibilities: static linking, load-time dynamic linking, and run-time dynamic linking. The other question is asking about the difference between the second and third, while this is talking about the difference between the first and a group of the second and third taken together. – Jerry Coffin Jan 14 '12 at 18:29

5 Answers5

15

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.

Andrey Rubshtein
  • 20,795
  • 11
  • 69
  • 104
Dexter
  • 2,482
  • 27
  • 40
  • This response would fit nicely in [c\# \- What do 'statically linked' and 'dynamically linked' mean? \- Stack Overflow](https://stackoverflow.com/questions/311882/what-do-statically-linked-and-dynamically-linked-mean) therefore this question is a duplicate. – Sam Hobbs Feb 14 '20 at 23:42
12

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

Will Dean
  • 39,055
  • 11
  • 90
  • 118
  • [Advantages and disadvantages of static and dynamic linking](http://www.engineeous.com/prepnote/static-linking-and-dynamic-linking) – Aneesh Oct 17 '12 at 19:37
2

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

0

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.

andrewsi
  • 10,807
  • 132
  • 35
  • 51
-1

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.

Makyen
  • 31,849
  • 12
  • 86
  • 121
Linuxdev
  • 1
  • 2
  • Excessive promotion of a specific product/resource may be perceived by the community as **spam**. Take a look at the [help], specially [What kind of behavior is expected of users?](//stackoverflow.com/help/behavior)'s last section: _Avoid overt self-promotion_. You might also be interested in [How to not be a spammer](//stackoverflow.com/help/promotion) and [How do I advertise on Stack Overflow?](http://stackoverflow.com/help/advertising). – double-beep Mar 12 '19 at 17:55
  • @Makyen thanks for the suggestions, this was my first post. Corrected the answers, please check and let me know if any feedback / suggestions. – Linuxdev Mar 12 '19 at 17:57