Static and Shared Libraries in C
Static and Shared Libraries in C
and
Shared
Libraries
in C
Table of Contents
Table of Contents
1. Introduction
2. What is a Static Library?
3. What is a Shared Library?
4. Static vs Shared Libraries
5. How to Create a Static Library
6. How to Create a Shared Library
7. Debugging and Troubleshooting
8. Conclusion
1. Introduction
1. Introduction
Advantages:
Faster execution since all code is available
in the final binary.
No dependency issues — the library is built
into the executable.
Simpler deployment — the binary is
standalone.
Disadvantages:
Larger binary size due to embedded library
code.
Increased memory consumption if multiple
programs use the same static library.
3. What is a
Shared Library?
3. What is a Shared Library?
Advantages:
Smaller binary size.
Lower memory consumption — multiple
programs can share the same library in
memory.
Easier updates — updating the shared
library automatically affects all dependent
programs.
Disadvantages:
Dependency issues — if the shared library
is missing or incompatible, the program may
fail.
Slight performance overhead due to
runtime linking.
4. Static vs
Shared Libraries
4. Static vs Shared Libraries
Key Differences
math.h
5. How to Create a Static Library
math.h
string_utils.h
6. How to Create a Shared Library
Explanation:
• -shared → Generates a shared library.
• -o string_utils.dll → Specifies the output
shared library file.
• -Wl,--out-implib,libstring_utils.a → Creates
an import library (libstring_utils.a) that
allows other programs to link against the
DLL.
DLL.
6. How to Create a Shared Library
Explanation:
• -L. → Tells the linker to search for libraries
in the current directory.
• -lstring_utils → Links against the import
library (libstring_utils.a).
• The generated program.exe will depend on
string_utils.dll at runtime.
6. How to Create a Shared Library
ldd program.exe
gdb program.exe
8. Conclusion
8. Conclusion