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