Creating a User-Defined Library in C

How to create User Defined  Library in C.

              In C programming, a user-defined library is a collection of user-created functions, variables, and data types that can be reused across multiple programs. These libraries are typically created to modularize code, improve reusability, and make programs easier to maintain.

To create and use a user-defined library, you need to:

 

  1. Create a header file (.h) that declares the functions, variables, and data types.

  2. Create a source file (.c) that implements the functions declared in the header file.

  3. Use the library in your main program (.c)  by including the header file and linking the object file during compilation.

Example: User-Defined Library

In this simple example we will create a user defined library containing two functions i)  add() for addition ii) subtract() for subtraction.

Step 1: Create the Header File (mylib.h)

The header file contains the function declarations (prototypes) and any necessary macros or data types.  Save this file  as ‘mylib.h’ in your working directory ( current directory)

 
// mylib.h
#ifndef MYLIB_H
#define MYLIB_H

// Function declaration
int add(int a, int b);
int subtract(int a, int b);

#endif
  • #ifndef MYLIB_H
    • The #ifndef directive checks if the identifier MYLIB_H has not been defined yet.
    • If MYLIB_H is not defined, the code following it (until #endif) will be included.
  • #define MYLIB_H
    • This line defines the identifier MYLIB_H to ensure the file’s contents are only included once.
  • #endif
    • Marks the end of the conditional block.

 

  • Step 2: Create the Source File (mylib.c)

    The source file contains the implementation of the functions declared in the header file.  Save this file  as ‘mylib.c’ in your working directory ( current directory)

     
    // mylib.c
    #include "mylib.h"
    
    // Function to add two numbers
    int add(int a, int b) {
        return a + b;
    }
    
    // Function to subtract two numbers
    int subtract(int a, int b) {
        return a - b;
    }

     

    Step 3: Use the Library in Your Main Program

    Create a main program that uses the functions from the library. You can name this file as ‘user_defined_lib.c’ and save it in the same directory.

     
    // user_defined_lib.c
    #include <stdio.h>
    #include "mylib.h"  // Include the user-defined library
    
    int main() {
        int x = 10, y = 5;
    
        printf("Addition: %d\n", add(x, y));
        printf("Subtraction: %d\n", subtract(x, y));
    
        return 0;
    }
     

Compile and Link the Main Program with the Library

Compile the main program and link it with the object file of the library.

 

gcc user_defined_lib.c mylib.c -o user_defined_lib

 

This creates an executable file named user_defined_lib.


Run the Program

Execute the program:

 
./user_defined_lib

Output:

 
Addition: 15
Subtraction: 5

You can also use the other way to comple your program 

  1. Compile the mylib.c file into an object file:

     
    gcc -c mylib.c -o mylib.o
  2. Compile the user_defined_lib.c file and link it with the mylib.o file:

     
    gcc user_defined_lib.c mylib.o -o user_defined_lib

    Run the program:

     
    ./user-defined_lib