Extract Object Files From Static Library on Linux

Extract Object Files From Static Library on Linux

When working with software development on Linux, you may encounter static libraries (files ending in .a). These archives bundle together multiple compiled object files into a single package. Sometimes it’s useful to pull out specific object files - whether for inspection, debugging, or reuse. This tutorial shows how to extract object files from static library on Linux.

Extract all files

It's good practice to keep the extracted files separate from the project tree. So, create a directory for extraction:

mkdir extracted

The ar tool, included in Binutils package, offers the -x option to extract object files from a static library.

For example, to unpack all object files from the /usr/lib/x86_64-linux-gnu/libz.a static library into the extracted directory, run the command:

ar -x /usr/lib/x86_64-linux-gnu/libz.a --output extracted

After this, you can check the directory (ls extracted) to confirm the object files were placed there. Example output:

adler32.o  compress.o  crc32.o  deflate.o  gzclose.o  gzlib.o  gzread.o  gzwrite.o
infback.o  inffast.o  inflate.o  inftrees.o  trees.o  uncompr.o  zutil.o

Extract only specific files

Sometimes you don't need to extract the entire archive. To pull out only certain object files, specify their names as command arguments. For example:

ar -x /usr/lib/x86_64-linux-gnu/libz.a adler32.o crc32.o --output extracted

Leave a Comment

Cancel reply

Your email address will not be published.