0% found this document useful (0 votes)
27 views29 pages

Static and Shared Libraries in C

The document provides an overview of static and shared libraries in C programming, detailing their definitions, advantages, and disadvantages. It includes practical steps for creating both types of libraries and emphasizes the importance of choosing the right library type based on project requirements. Additionally, it covers debugging techniques and concludes with a summary of the key differences between static and shared libraries.

Uploaded by

m
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views29 pages

Static and Shared Libraries in C

The document provides an overview of static and shared libraries in C programming, detailing their definitions, advantages, and disadvantages. It includes practical steps for creating both types of libraries and emphasizes the importance of choosing the right library type based on project requirements. Additionally, it covers debugging techniques and concludes with a summary of the key differences between static and shared libraries.

Uploaded by

m
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

Static

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

In C programming, libraries play a crucial role


in organizing code and promoting reusability.
Libraries allow developers to store frequently
used functions or modules in one place,
making the codebase cleaner and more
efficient.
In C, there are two main types of libraries:
• Static libraries – Linked at compile time,
making the library part of the final
executable.
• Shared libraries – Linked at runtime,
reducing executable size and allowing code
reuse.

Understanding the differences between static


reuse. 1. Introduction

Understanding the differences between static


and shared libraries is essential for writing
efficient and maintainable C programs. In this
article, we’ll explore how to create and use
both static and shared libraries with practical
examples.
2. What is a Static
Library?
2. What is a Static Library?

Definition and Purpose


A static library is a collection of compiled object
files bundled into a single file (.a) that is linked
with the program at compile time. The code in
a static library is copied into the executable
when the program is compiled.

How Static Libraries Are Linked at Compile


Time?
When you compile a program with a static
library, the linker pulls the required object files
from the static library and includes them in the
final binary.
2. What is a Static Library?

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?

Definition and Purpose


A shared library (.so on Linux or .dll on
Windows) is a collection of object files that are
linked to the program at runtime. The
executable only stores references to the
shared library.

How Shared Libraries Are Linked at


Runtime
At runtime, the dynamic linker (ld.so) loads the
shared library into memory and resolves the
references.
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

When to Use Static vs Shared Libraries


• Use static libraries when performance and
portability are critical.
• Use shared libraries when reducing memory
usage and easier updates are more
important.
5. How to Create a
Static Library
5. How to Create a Static Library

Example: Creating a Math Library


1. Create the source files
math.c

math.h
5. How to Create a Static Library

math.h

2. Compile the source files into object files

3. Create a static library using ar

4. Link the static library with a program


5. How to Create a Static Library

4. Link the static library with a program


main.c

Compile and link:

5. Run the program


program.exe
6. How to Create a
Shared Library
6. How to Create a Shared Library

Example: Creating a String Library


1. Create the source files
string_utils.c

string_utils.h
6. How to Create a Shared Library

2. Compile the Source Files into Object


Files
6. How to Create a Shared Library

3. Create the Shared Library


Next, create a shared library (.dll) from the
object file using the -shared option:

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

4. Create a Program That Uses the Shared


Library
main.c
6. How to Create a Shared Library

5. Link the Shared Library to the Program


Use the import library (libstring_utils.a) when
compiling the main program:

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

6. Run the program


Make sure the shared library (string_utils.dll) is
available in the same directory as the
executable (or in a directory included in the
PATH environment variable):
program.exe
7. Debugging and
Troubleshooting
7. Debugging and Troubleshooting

Use ldd to check shared library


dependencies:

ldd program.exe

Use gdb to debug:

gdb program.exe
8. Conclusion
8. Conclusion

Static and shared libraries each offer distinct


benefits. Static libraries provide better
performance and portability, while shared
libraries offer smaller memory footprints and
easier updates. The choice depends on the
project’s specific requirements — whether you
need higher performance or greater flexibility.
Understanding how to create and link both
types of libraries is essential for writing efficient
and scalable C programs.

You might also like