Introduction
A .a file is a static library on Linux, macOS and other Unix-like
systems: a bundle of object files (.o, in
ELF or Mach-O
format) packed with the ar archiver. When a program is linked against it,
the linker copies in only the object files that define something the program uses.
Library names follow the pattern libname.a, which the option
-lname finds.
The ar archive format is older than static libraries and is used for other things
too: a Debian package is an ar archive, and Windows
static libraries (.lib) use a variant of
it.
Structure
The file starts with the eight bytes !<arch>\n. Each member file
follows, with a 60-byte header of plain ASCII fields padded with spaces: the name, the
modification time, owner and group IDs, the file mode in octal, the size in decimal,
and the two bytes `\n. The member’s contents come next, padded to an even
length.
For a library, the first member is a symbol index that maps each global symbol to the
member defining it, so the linker need not read every object file. In the GNU and
System V variant it is named /, and a member named // holds
file names longer than 15 characters; the BSD and macOS variant names the index
__.SYMDEF and stores long names in the member itself. The
ranlib tool, or ar s, rebuilds the index.
0signature!<arch>\n8 bytes8member header: name, date, uid, gid, mode, size,`\n60 bytes68symbol index member, such as/or__.SYMDEFvaries
…member header and object file, repeated60 bytes + size
Links
Software
- GNU Binutils, with
ar,ranlibandnm(Linux, macOS, Windows) - LLVM, with
llvm-arandllvm-nm(Linux, macOS, Windows) - Xcode, with
arandlibtool(macOS) - 7-Zip, which can list and extract the members (Windows)
Developers
- object, Rust crate that reads ar archives and the object files inside
Reference
- ar(5), the FreeBSD manual page for the archive format and its variants