Dynamic Libraries
Lesson 8.4: Dynamic Libraries
In this lesson, you'll delve into dynamic libraries, a powerful way to modularize your C code and make it more reusable and maintainable. Dynamic libraries are separate files containing compiled code and data that your programs can load and use at runtime.
What Are Dynamic Libraries?
Shared Code: Dynamic libraries (also called shared libraries or dynamically linked libraries) contain functions and data that can be shared by multiple programs.
Runtime Linking: These libraries are linked to your program at runtime, meaning the linking process happens when your program is executed, not during compilation.
Benefits:
Reduced Executable Size: Your executables become smaller because the library code isn't embedded within them.
Memory Efficiency: Multiple programs can share the same library code in memory, saving resources.
Easier Updates: You can update the library without recompiling all the programs that use it.
How Dynamic Libraries Work
Compilation: Your library code is compiled into a separate file with a specific extension (e.g., .so on Linux/macOS, .dll on Windows).
Linking (Runtime): When your main program runs, it dynamically loads the library and resolves references to the functions and data it needs. This can be done explicitly (using functions like dlopen and dlsym) or implicitly (through linker options).
Execution: Your program can now use the functions and data from the loaded library as if they were part of its own code.
Creating a Dynamic Library
Write Library Code: Create a C source file (mylib.c) containing the functions and data you want to include in your library.
Compile: Use the -fPIC (Position Independent Code) flag to make your code suitable for dynamic linking:
Bash
gcc -c -Wall -Werror -fPIC mylib.c -o mylib.o
Use code with caution.
content_copy
Link: Create the dynamic library using the following command (specific flags might vary depending on your platform):
Bash
gcc -shared -o libmylib.so mylib.o
Use code with caution.
content_copy
Using a Dynamic Library
Include Header File: Include the header file (mylib.h) for your library in your main program to access function prototypes and data declarations.
Link (Implicit):
Linux/macOS: When compiling your main program, use the -l flag to link with the library:
Bash
gcc main.c -L. -lmylib -o main
Use code with caution.
content_copy
(-L. tells the linker to search in the current directory for libraries.)
Windows: Use the -l flag and specify the full path to the library file.
Important Considerations
Library Naming Conventions:
Linux/macOS: Typically have the prefix lib and the extension .so (e.g., libmylib.so).
Windows: Usually have the extension .dll (e.g., mylib.dll).
LD_LIBRARY_PATH (Linux/macOS): You might need to set the LD_LIBRARY_PATH environment variable to tell the system where to find your library.
Example: Dynamic Library in Action
C
// mylib.h (header file)
int add(int a, int b);
// mylib.c (library code)
int add(int a, int b) {
return a + b;
}
// main.c (main program)
#include <stdio.h>
#include "mylib.h"
int main() {
int result = add(5, 3);
printf("Result: %d\n", result);
return 0;
}
Let me know if you have any other questions.