How Do I Create a Library in C?
Last Updated :
17 Jun, 2024
Libraries are a collection of code that can be used by other programs. They promote code reuse and modularity. In C, we can create our own libraries. In this article, we will learn how to create a library in C.
Creating a Library in C
In C, there are multiple methods using which we can create a library.:
- Static Library
- Dynamic Library
In this article, we will look at the static libraries. Before C++11, copying objects was the only way to transfer data, which could be slow for large objects. Move semantics allows transferring ownership of resources without copying.
Static Library
A static library is a collection of object files that are linked into the program during the linking phase of compilation, resulting in a single executable file.
STEP 1: Define header file.
Create the source files for your library. For example, mylib.c and mylib.h.
mylib.h
C++
#ifndef MYLIB_H
#define MYLIB_H
void hello();
#endif // MYLIB_H
STEP 2: Create Source File for header
C
#include <stdio.h>
#include "main"
using namepace std;
void hello() {
cout << "Hello World";
}
STEP 3. Compile the Object Files:
Compile our source files into object files (.o or .obj).
gcc -c mylib.c -o mylib.o
STEP 4: Create the Static Library:
Use the ar (archiver) command to create the static library (.a file).
ar rcs libmylib.a mylib.o
STEP 5. Use the Static Library:
Link the static library to our program when compiling it.
C
// main.c
#include "mylib.h"
int main()
{
hello();
return 0;
}
gcc main.c -L. -lmylib -o myprogram
Shared (Dynamic) Library
A shared library is a collection of object files that are linked dynamically at runtime by the operating system.
1. Write Your Code:
Use the same source files as in the static library example.
2. Compile the Object Files with Position-Independent Code (PIC):
Compile your source files into position-independent object files.
gcc -c -fPIC mylib.c -o mylib.o
3. Create the Shared Library:
Use the gcc command to create the shared library (.so file).
gcc -shared -o libmylib.so mylib.o
4. Use the Shared Library:
Link the shared library to your program when compiling it.
C
// main.c
#include "mylib.h"
int main()
{
hello();
return 0;
}
gcc main.c -L. -lmylib -o myprogram
5. Run the Program with the Shared Library:
Ensure the shared library is in our library path or use the LD_LIBRARY_PATH environment variable to specify the path.
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:../myprogram
Explanation:
- Headers (.h files): Contain function declarations and macro definitions to be shared between several source files.
- Source (.c files): Contain the actual code of the functions.
- Object files (.o files): Compiled code that is not yet linked into an executable.
- Static library (.a file): Archive of object files.
- Shared library (.so file): Dynamically linked library.
By following these steps, you can crea
te both static and shared libraries in C.
Similar Reads
How to Create a Dynamic Library in C++? In C++, dynamic libraries also known as shared libraries are a powerful way to modularize your code. They allow the users to build libraries that can be loaded at runtime by multiple applications at once. This approach promotes code reuse, reduces code duplication, and simplifies maintenance of the
4 min read
Rust - Creating a Library Rust is a multi-paradigm programming language like C++ syntax that was designed for performance and safety, especially for safe concurrency. Also, it is a compiled system programming language. In this article, we will see how to create libraries in Rust. Creating a Rust Library:Step 1: We start by c
1 min read
Creating and Using DLL (Class Library) in C# A class library file is a collection of classes and namespaces in C# without any entry point method like Main. Once we create a class library file it can be used in the C# project and classes inside it can be used as required. Class Library makes it convenient to use functionalities by importing DLL
4 min read
How to Install a C# Class Library in Visual Studio? A C# library project is a separate project used to hold utility classes. So this might be the class that handles our database or might handle some communications with the network. In our case, we are going to create a math library that is a stand-in for some of those other cases. It is a single sour
3 min read
SDL library in C/C++ with examples SDL is Simple DirectMedia Layer.It is a cross-platform development library designed to provide low level access to audio, keyboard, mouse, joystick, and graphics hardware via OpenGL and Direct3D.It can be used to make animations and video games. It basically provides a set of APIs to interact with v
4 min read