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
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
How To Compile And Run a C/C++ Code In Linux C Programming Language is mainly developed as a system programming language to write kernels or write an operating system. C++ Programming Language is used to develop games, desktop apps, operating systems, browsers, and so on because of its performance. In this article, we will be compiling and exe
4 min read
How to add "graphics.h" C/C++ library to gcc compiler in Linux While trying c graphic programming on Ubuntu, I figured out that graphic.h is not a standard C library and it is not supported by gcc compiler. So I am writing this article to explain the process.If you want to use graphics.h on Ubuntu platform you need to compile and install libgraph. It is the imp
2 min read
Difference between Header file and Library Header Files: The files that tell the compiler how to call some functionality (without knowing how the functionality actually works) are called header files. They contain the function prototypes. They also contain Data types and constants used with the libraries. We use #include to use these header
3 min read