In Linux development, you'll often deal with static libraries - archives ending with .a
. These files package several compiled object files (.o
) into a single unit, which makes distribution and linking easier. At times, you may want to create your own static library from a set of object files. This tutorial explains how to create static library from object files on Linux.
Specific object files
The ar
(part of the Binutils package) is the standard tool for creating archive libraries. To create a new static library from object files, we can use rcs
option. For example, the following command creates a new static library named mylib.a
that contains selected object files:
ar rcs mylib.a compress.o zutil.o uncompr.o
- r - replace or add the files into the archive.
- c - create the archive if it doesn't exist.
- s - build an index for faster linking.
All object files at once
If you'd like to include every .o
file, you can use a wildcard *.o
:
ar rcs mylib.a *.o
This bundles every .o
file in the current directory into the static library.
Leave a Comment
Cancel reply