Logo

dev-resources.site

for different kinds of informations.

Static vs dynamic linking

Published at
11/18/2022
Categories
programming
linux
computerscience
compilers
Author
goamaral
Author
8 person written this
goamaral
open
Static vs dynamic linking

What is static linking?

Static linking links libraries at compile time, copying them to the final binary.

What is dynamic linking?

Dynamic linking loads and links libraries at runtime, loading them to memory.

Only the name of the shared libraries is saved at compile time.

These names are saved in a PLT (Procedure Linkage Table)

Static vs dynamic linking

Static

  • Bigger binaries

Dynamic

  • Depend on external libraries to be installed and be compatible
  • Shared libraries are shared across processes
  • Shared library code can be updated/patched without new compilation
  • Updates to shared library code can add breaking changes and prevent the program from running

How to create a statically linked binary?

$ ld [options] objfile
Enter fullscreen mode Exit fullscreen mode

ld combines several object and archive files, relocates their data and ties up symbol references. Usually, the last step in compiling a program is to run ld.

$ gcc hello.c -static -o hello
Enter fullscreen mode Exit fullscreen mode

How to create a dynamically linked binary?

$ gcc hello.c -o hello
Enter fullscreen mode Exit fullscreen mode

How to know if a binary is statically or dynamically linked?

Check the type of linking

$ file /usr/bin/gcc

/usr/bin/gcc: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=017fc52acbca077c9bc6a4e8f04dd90eb5385243, for GNU/Linux 4.4.0, stripped
Enter fullscreen mode Exit fullscreen mode

Check dynamically linked libraries

$ ldd /bin/gcc

linux-vdso.so.1 (0x00007fff6377e000)
libc.so.6 => /usr/lib/libc.so.6 (0x00007fcd238f2000)
/lib64/ld-linux-x86-64.so.2 => /usr/lib64/ld-linux-x86-64.so.2 (0x00007fcd23b02000)
Enter fullscreen mode Exit fullscreen mode

Cover image icons by icons8.com

compilers Article's
30 articles in total
Favicon
How to create simple tool for compile the Linux Kernel
Favicon
Unraveling Undefined Behavior: Performance Optimizations in Modern Compilers
Favicon
Video — Deep dive: Compiling deep learning models, from XLA to PyTorch 2
Favicon
The current state of Lithia after 2 years
Favicon
Verificando e Gerando Expressões
Favicon
Expressões encadeadas e agrupamento
Favicon
Improving Compiler Performance with Profile Guided Optimization
Favicon
Understanding Interpreters and Compilers in Programming
Favicon
Create Your Own Programming Language 9: Iteration
Favicon
Crafting Interpreters
Favicon
Create Your Own Programming Language 8: Conditionals
Favicon
Create Your Own Programming Language 7: More Types
Favicon
Create Your Own Programming Language 6: Functions
Favicon
Create Your Own Programming Language 4: Variables and Types
Favicon
Create Your Own Programming Language 3: Call Expressions
Favicon
How To Create Your Own Programming Language
Favicon
Create Your Own Programming Language 1: Numbers
Favicon
An alternative to "Distinguishing an Interpreter from a Compiler"
Favicon
On What Lexers Do
Favicon
Compilers Could Be Way More Fun
Favicon
Incremental compilation for Crystal - Part 3
Favicon
Incremental compilation for Crystal - Part 1
Favicon
Incremental compilation for Crystal - Part 2
Favicon
Crafting a Compiler in Rust: Lexical Analysis
Favicon
Crafting a Compiler in Rust: Introduction
Favicon
What is an ELF file?
Favicon
Static vs dynamic linking
Favicon
Speeding up ReScript compilation using interface files
Favicon
🕶 What it takes to build a Static Analysis tool
Favicon
A Compiler optimization area

Featured ones: