Dynamic Libraries

Dynamic Libraries

Dynamic libraries (also known as shared libraries or DLLs - Dynamic Link Libraries) in C provide a way to share code among multiple programs and reduce the overall size of executable files. They are compiled separately from the main program and loaded at runtime. Here are the basic concepts and steps involved in working with dynamic libraries in C:

Creating Dynamic Libraries:

  1. Write the Library Code:

    • Create the source code for the functions you want to include in the dynamic library.
  2. Compile the Library:

    • Compile the source code into a dynamic library. On Unix-like systems, this is often done with the -shared flag.

        gcc -shared -o dlibrary.so dlibrary.c
      
  3. Useful Compiler Flags:

    • -fPIC (Position Independent Code): Required for creating position-independent code, necessary for shared libraries.

    • -shared: Indicates that a shared library should be created.

Using Dynamic Libraries in a C Program:

  1. Include Header Files:

    • Include the necessary header files in your C program.

        #include <stdio.h>
        #include "dlibrary.h"  // Header file for your library
      
  2. Load the Library:

    • Use dynamic loading functions to load the library at runtime.

        #include <dlib.h>
      
        void *handle = dlopen("mylibrary.so", RTLD_LAZY);
        if (!handle) 
        {
            fprintf(stderr, "Error loading library: %s\n", dlerror());
            return 1;
        }
      
  3. Get Function Pointers:

    • Obtain function pointers for the functions you want to use from the library.

        // Function pointer
        typedef int (*MyFunctionType)(int);
      
        // Get function pointer
        MyFunctionType myFunction = (MyFunctionType)dlsym(handle, "myFunction");
      
  4. Use the Functions:

    • Call the functions as if they were regular functions.

        int result = myFunction(42);
        printf("Result: %d\n", result);
      
  5. Close the Library:

    • Remember to close the library when you are done.

        dlclose(handle);
      

Example:

Here's a simple example:

  1. Library Code (dlibrary.c):

     // dlibrary.c
     #include <stdio.h>
    
     int myFunction(int x) {
         return x * x;
     }
    
  2. Compile Library:

     gcc -shared -fPIC -o dlibrary.so dlibrary.c
    
  3. Main Program:

     // main.c
     #include <stdio.h>
     #include <dlfcn.h>
    
     int main() {
         void *handle = dlopen("dlibrary.so", RTLD_LAZY);
         if (!handle) {
             fprintf(stderr, "Error loading library: %s\n", dlerror());
             return 1;
         }
    
         typedef int (*MyFunctionType)(int);
         MyFunctionType myFunction = (MyFunctionType)dlsym(handle, "myFunction");
    
         if (!myFunction) {
             fprintf(stderr, "Error getting function: %s\n", dlerror());
             dlclose(handle);
             return 1;
         }
    
         int result = myFunction(5);
         printf("Result: %d\n", result);
    
         dlclose(handle);
         return 0;
     }
    
  4. Compile Main Program:

     gcc -o myprogram main.c -ldl
    
  5. Run:

     ./myprogram
    

This is a basic overview of working with dynamic libraries in C. Note that the process might vary slightly depending on the platform and the development environment you are using.