Difference Between .a and .so files

A programmer may want to write three different programs. However, he realizes that some of the functionality needed for each program could be shared among them. Therefore, he decides to create a library containing these shared features.

A library is basically a collection of code and data that other people can use. On Linux, archives (with the .a file extension) contain compiled object code, whereas shared objects (.so files) contain code that can be dynamically loaded at runtime.

Here, we'll examine how software runs under Linux and the purposes of the library and archive files. We'll also see some examples of how we can create these files for our own applications using the GCC compiler and the GNU ar utility.

How Programs Run Under Linux

Most Linux systems have the /lib and /usr/lib directories. These are the directories that contain all the common functions that are needed by the programs installed on the computer. A convention states that a library name begins with lib and the extension indicates the type of the file

.a ? stands for "archive" (static library)
.so ? stands for "shared object" (dynamic library)

A program may depend on multiple shared objects. When we run an application, it will look for any required dependencies in the /lib and /usr/lib folders. If the required dependencies aren't installed, the program won't run. Package managers help calculate and determine these dependencies before installing software.

Static Libraries (.a files)

A static library is a collection of source files compiled into an archive file that can be used by a compiler during compilation. When linking against such a library, the linker takes the archive file and places the required object code directly within the final application binary.

Static libraries usually end with the .a extension for example, glibc.a. The key characteristics are

  • Code is embedded directly into the executable at compile time

  • Larger executable file sizes

  • No external dependencies at runtime

  • Each program gets its own copy of the library code

Shared Libraries (.so files)

Shared libraries (also known as dynamic libraries) are loaded dynamically at runtime. These files are linked into an application when it runs, not when it's compiled.

Shared libraries usually end in .so (for shared object) for instance, libboost_filesystem.so. Unlike static libraries, programs referencing dynamic libraries don't include their corresponding objects when they're built into an executable, resulting in smaller executables.

When multiple applications reference the same shared library, the library has one copy in memory that can be used by all of them simultaneously. Here's an example from the xorg directory

$ ls -halF /usr/lib/xorg/modules/
-rwxr-xr-x 1 root root 95K Apr 13 20:12 libexa.so*
-rwxr-xr-x 1 root root 23K Apr 13 20:12 libfbdevhw.so*
-rwxr-xr-x 1 root root 111K Apr 13 20:12 libfb.so*
-rwxr-xr-x 1 root root 213K Apr 13 20:12 libglamoregl.so*

Comparison

Feature Static Libraries (.a) Shared Libraries (.so)
Linking Time Compile time Runtime
Executable Size Larger Smaller
Memory Usage Higher (duplicate copies) Lower (shared in memory)
Dependencies Self-contained Requires library files
Performance Slightly faster Slight overhead from dynamic loading

Building Static Libraries (.a)

To create a static library, first compile the source files into object files

$ gcc -Wall -c *.c

The -Wall flag enables all warnings, and -c compiles without linking. This creates .o object files. Next, use the ar utility to create the archive

$ ar -cvq libfile.a *.o

The flags mean: c (create), v (verbose), q (quick append). To view the contents of the library

$ ar -t libfile.a

To use the static library in compilation

$ gcc -o MyProgram main.c -L./path/to/lib -lfile

Building Shared Libraries (.so)

First, compile source files with position-independent code

$ gcc -Wall -fPIC -c *.c

The -fPIC flag generates position-independent code, which is essential for shared libraries. Then create the shared library

$ gcc -shared -o libfile.so *.o

The -shared flag specifies that we're building a shared object file. To use the shared library

$ gcc -o MyProgram main.c -L./path/to/lib -lfile

Conclusion

Static libraries (.a files) embed code directly into executables at compile time, creating self-contained but larger binaries. Shared libraries (.so files) are loaded at runtime, enabling smaller executables and memory sharing between programs. Both serve important roles in Linux software development and system organization.

Updated on: 2026-03-17T09:01:38+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements