How to Create and Compile a Static Library in Linux (2026)

Brief: This article guide provides an understanding between static and dynamic libraries and thereafter demonstrates how a user can create a static library in Linux via the aid of the Linux ar command.

During the compilation process of a C program, an object code is usually the end result of a successful compiler runtime. Afterward, a linker is invoked to make the library function code available to a user program. The linker accomplishes this objective either by making the library function code copy available to the user object code or by availing the library function code at run-time.

When the linker creates a copy of these re-usable library function codes and passes/avails them to an executable program file, a static library is born. It’s called a static library because the libraries and/or library files are statically linked. Static libraries in Linux are identified by the '.a' file extension.

Features of a Static Library

Before we create a static library in Linux, we should note the following advantageous features:

  • Reduced Size – They are smaller in size in comparison to shared libraries hence advantageous to limited storage space systems.
  • Improved Performance – During runtime, shared libraries are associated with the overhead of loading (tracing a shared library location, verifying its version, and mapping it to memory for an executable program to access it). With the static library, all the necessary code and data are readily availed to the executable program.
  • Improved Portability – Static libraries’ linked executables are easy to deploy and run since they do not depend on external runtime environments or libraries. They are therefore adaptable to different systems, unlike shared libraries.
  • Improved Stability – The executable program’s stability is likely improved since it is bundled with the static libraries which prevent the library from being modified or updated while on a different system environment (a key cause for compatibility issues).

Creating a Static Library in Linux

We will accomplish the creation of our static library by covering the following steps:

Step 1: Create a C File

The first step is to create a C file that will accommodate all the needed library functions.

$ nano mylibrary.cOR$ vi mylibrary.c

We will use the following code for simplicity reasons:

/* Filename: mylibrary.c */#include <stdio.h>void task(void){printf("task() called from a static library");}

Step 2: Create a Library Header File

The next step is to create the library header file:

$ nano mylibrary.hOR$ vi mylibrary.h

The code to use is:

/* Filename: mylibrary.h */void task(void);

Step 3: Create an Object File

The next step is to compile the above library files and create an object file mylibrary.o using a gcc compiler.

$ gcc -c mylibrary.c -o mylibrary.o
How to Create and Compile a Static Library in Linux (1)

The -c flag compiles the C library and the -o flag creates the library object. You can create multiple library objects from different C files.

Step 4: Create a Static Library

Once you have the needed number of library objects, you can proceed to create a static library. Here, we will borrow the functionality of the Linux ar Command.

$ ar rcs mylibrary.a mylibrary.o

The above command creates a static library called mylibrary.a. You can also include multiple library objects in the following command format:

$ ar rcs mylibrary.a lib1.o lib2.o lib3.o … 
How to Create and Compile a Static Library in Linux (2)

The r option creates an archive of the provided library object files, c option initiates the archiving process and s option creates the archive’s index (symbol table).

The created static library file (mylibrary.a) is now portable and ready for use.

How to Use Static Library in Linux

To demonstrate the usage of this static library, let us create a driver program with the help of a C file.

$ nano driver.cOr$ vi driver.c

Add the main function using the following code:

/* filename: driver.c */#include "mylibrary.h"void main(){task();}

Create the library object from this C file:

$ gcc -c driver.c -o driver.o
How to Create and Compile a Static Library in Linux (3)

We will now use the generated library object to create a driver program and link it to our static library.

$ gcc -o driver driver.o -L. -l:mylibrary.a

The -L. point to the location of the static library i.e current folder and -l:mylibrary.a creates a static link between the static library and the driver program.

We can now test the execution of this driver program:

$ ./driver
How to Create and Compile a Static Library in Linux (4)

The output “task() called from a static library” originating from the C file we created earlier confirms that the driver program and the static library are statically linked.

Modifying Static C Libraries

In reference to the above-covered steps that led to the creation of a static library (mylibrary.a), we can conclude that the creation of a static library only requires the existence of valid object files (library objects). Therefore, we can redefine a static library as a composition of library objects.

Viewing the Content of a Static Library

Suppose, we want to display all the library objects that make up our static library, we will use the ar command:

$ ar t mylibrary.aor$ ar tv mylibrary.a 
How to Create and Compile a Static Library in Linux (5)

The -t command option lists all the object files present and -v is for verbose display and provides a detailed preview that includes file attributes like owner, group, timestamp, and permission.

Extracting Library Object from Static Library

If you need to extract and inspect a library object from a static library, utilize the ar command option x. We can also combine the command with the verbose option (v) for the interactive execution of the command.

The following command extracts all available library objects.

$ ar xv mylibrary.a

The following command extracts a single specified library object.

$ ar xv mylibrary.a mylibrary.o 

The following command extracts multiple specified library objects.

$ ar xv mylibrary.a lib1.o lib2.o lib3.o

Adding Library Object to Static Library

Supposing the static library mylibrary.a needs to accommodate additional library objects, we will make use of the ar command option r. We can even combine it with the verbose option.

This command adds a single specified library object to the static library

$ ar rv mylibrary.a mylibrary.o 

This command adds multiple specified library objects to the static library.

$ ar rv mylibrary.a lib1.o lib2.o lib3.o

Afterward, view the content of the static library to confirm the addition of the selected library objects.

$ ar tv mylibrary.a 

Deleting Library Objects from Static Library

If you want to delete single or multiple library objects from a static library, make use of the ar command option d. Combine it with the verbose option to see the library object files being deleted.

This command deletes a single specified library object from the static library

$ ar dv mylibrary.a mylibrary.o 

This command deletes multiple specified library objects from the static library.

$ ar dv mylibrary.a lib1.o lib2.o lib3.o
Conclude

A major advantage associated with static libraries is their ability to accelerate the program execution process. However, if the static library undergoes any changes, a user will have to every time recompile the main program for the changes to take effect.

That said, you can now employ the concept of static libraries in your current and future Linux projects.

How to Create and Compile a Static Library in Linux (2026)

FAQs

How to create a static library in Linux? ›

Creating a Static Library in Linux
  1. Step 1: Create a C File. The first step is to create a C file that will accommodate all the needed library functions. ...
  2. Step 2: Create a Library Header File. ...
  3. Step 3: Create an Object File. ...
  4. Step 4: Create a Static Library.
Dec 4, 2023

Which command can be used to create static libraries? ›

Use the ar (archiver) command to create the static library (. a file).

How to make a static library in Makefile? ›

If present, the Makefile uses the “ar” command to create an archive file, which becomes our static library. The “r”, “c”, and “s” flags are used with the “ar” command to replace or add files to the archive, create the archive if it doesn't exist, and write an index into the archive for faster searches.

How to create a static library in gcc? ›

  1. To create a static library using gcc, First, we need to compile all our C function files into their object files. ...
  2. After creating the object files of our functions, we need to link them together in an archive, which is our library. ...
  3. After a library is created, or modified, there is a need to index it.
Oct 13, 2019

How are static libraries created? ›

Basically, static libraries are just a collection of object files that are merged by the linker with another object file to form a final executable. Conventionally, they start with “lib” and end with “. a” or “. lib” (depending on your platform).

How to make a Linux library? ›

Procedure
  1. Change to the directory with library sources.
  2. Compile each source file to an object file with the position-independent code option -fPIC : ...
  3. Link the shared library from the object files: ...
  4. Copy the libfoo.so.x.y file to an appropriate location where the system's dynamic linker can find it.

What is the command to create a library? ›

Creating a library

To create a library, use System i® Navigator or the Create Library (CRTLIB) command. The system-recognized identifier for the object type is *LIB. Before you can create a database file, you must create a library to store it.

How do I create a static library for an object file? ›

How to create it?: To create a static library you need the object files (*.o) of your code, to obtain them you need to compile your code using the flag -c to stop gcc before the linker step. With all the .o files, now you use ar -rc libName. a *.o to create the library.

What is the format of a static library? ›

Static libraries are simply a collection of ordinary object files; conventionally, static libraries end with the ``. a'' suffix. This collection is created using the ar (archiver) program.

How do I create a static file? ›

Configuring static files¶

In your templates, use the static template tag to build the URL for the given relative path using the configured staticfiles STORAGES alias. Store your static files in a folder called static in your app. For example my_app/static/my_app/example.jpg .

How do I create a static library to a shared library? ›

Therefore, we must use the -Wl,–whole-archive option to include the entire static library during linkage. We use the -Wl,–nowhole-archive flag to specify the end of used static libraries. We now see the generated shared library, libfoo.so. The nm command lists the symbols in an executable.

Where does Makefile look for libraries? ›

The make command itself does not search for libraries or header files - instead it looks for a Makefile in the current directory (unless an alternative file is specified on the command line using the -f option) and executes the instructions inside.

How do I create a static library in Unix? ›

Creating a static library

The flag -c instructs the gcc compiler to stop the compilation procedure at the generation of the object file (and not to continue to the linking phase that would generate an executable). Next, ar -cvq lib/libgreeter. a greeter.o finally generates the static library libgreeter.

What is a static library in Linux? ›

Static libraries are created by copying all necessary library modules used in a program into the final executable image. The linker links static libraries as a last step in the compilation process. An executable is created by resolving external references, combining the library routines with program code.

How to create a dynamic library in Linux? ›

How to Create a Dynamic Library (Linux) To create a dynamic library in Linux, simply type the following command: gcc *. c -c -fPIC and hit return. This command essentially generates one object file .o for each source file .

How do I create a static connection in Linux? ›

How to Set a Static IP Using the GUI
  1. Search for settings .
  2. Click on either Network or Wi-Fi tab, depending on the interface you would like to modify.
  3. To open the interface settings, click on the gear icon next to the interface name.
  4. Select “Manual” in the IPV4 tab and enter your static IP address, Netmask and Gateway.
Mar 2, 2023

How do I create a static route in Linux? ›

Add a static route on Linux
  1. $ su - OR use the sudo as follows:
  2. $ sudo -i. Once become a root user, setup a temporary route using the ip command:
  3. # ip route add 172.10.1.0/24 via 10.0.0.100 dev eth0. Verify new routing table, enter:
  4. # ip r. ...
  5. # ip link set dev tun0 up mtu 1500. ...
  6. # ip r.
Jun 28, 2024

Top Articles
Latest Posts
Recommended Articles
Article information

Author: Madonna Wisozk

Last Updated:

Views: 5519

Rating: 4.8 / 5 (48 voted)

Reviews: 87% of readers found this page helpful

Author information

Name: Madonna Wisozk

Birthday: 2001-02-23

Address: 656 Gerhold Summit, Sidneyberg, FL 78179-2512

Phone: +6742282696652

Job: Customer Banking Liaison

Hobby: Flower arranging, Yo-yoing, Tai chi, Rowing, Macrame, Urban exploration, Knife making

Introduction: My name is Madonna Wisozk, I am a attractive, healthy, thoughtful, faithful, open, vivacious, zany person who loves writing and wants to share my knowledge and understanding with you.