|This article needs additional citations for verification. (March 2011) (Learn how and when to remove this template message)|
The archiver, also known simply as ar, is a Unix utility that maintains groups of files as a single archive file. Today, ar is generally used only to create and update static library files that the link editor or linker uses; it can be used to create archives for any purpose, but has been largely replaced by tar for purposes other than static libraries. An implementation of ar is included as one of the GNU Binutils.
In the Linux Standard Base, ar has been deprecated and is expected to disappear in a future release of that Standard. The rationale provided was that "the LSB does not include software development utilities nor does it specify .o and .a file formats."
File format details
An ar file begins with a global header, followed by a header and data section for each file stored within the ar file.
Each data section is 2 byte aligned. If it would end on an odd offset, a newline ('\n', 0x0A) is used as filler.
The common format is as follows. Numeric values are encoded in ASCII and all values right-padded with ASCII spaces (0x20).
|16||12||File modification timestamp||Decimal|
|48||10||File size in bytes||Decimal|
|58||2||File magic||0x60 0x0A|
As the headers only include printable ASCII characters and line feeds, an archive containing only text files therefore still appears to be a text file itself.
The members are aligned to even byte boundaries. "Each archive file member begins on an even byte boundary; a newline is inserted between files if necessary. Nevertheless, the size given reflects the actual size of the file exclusive of padding."
Due to the limitations of file name length and format, both the GNU and BSD variants devised different methods of storing long filenames. Although the common format does not suffer from the year 2038 problem, many implementations of the ar utility do and may need to be modified in the future to handle correctly timestamps in excess of 2147483647.
BSD ar stores filenames right-padded with ASCII spaces. This causes issues with spaces inside filenames. 4.4BSD ar stores extended filenames by placing the string "#1/" followed by the file name length in the file name field, and storing the real filename in front of the data section.
BSD ar utility traditionally does not handle the building of a global symbol lookup table, and delegates this task to a separate utility named ranlib, which inserts an architecture-specific file named
__.SYMDEF at the beginning of the archive.
System V (or GNU) variant
System V ar uses a '/' character (0x2F) to mark the end of the filename; this allows for the use of spaces without the use of an extended filename. Then it stores multiple extended filenames in the data section of a file with the name "//", this record is referred to by future headers. A header references an extended filename by storing a "/" followed by a decimal offset to the start of the filename in the extended filename data section. The format of this "//" file itself is simply a list of the long filenames, each separated by one or more LF characters. Note that the decimal offsets are number of characters, not line or string number within the "//" file.
System V ar uses the special filename "/" to denote that the following data entry contains a symbol lookup table, which is used in ar libraries to speed up access. This symbol table is built in three parts which are recorded together as contiguous data.
- A 32-bit big endian integer, giving the number of entries in the table.
- A set of 32-bit big endian integers. One for each symbol, recording the position within the archive of the header for the file containing this symbol.
- A set of Zero-terminated strings. Each is a symbol name, and occurs in the same order as the list of positions in part 2.
The special file "/" is not terminated with a specific sequence; the end is assumed once the last symbol name has been read.
To create an archive from files class1.o, class2.o, class3.o, the following command would be used:
ar rcs libclass.a class1.o class2.o class3.o
cc main.c libclass.a
or (if libclass.a is placed in standard library path, like /usr/local/lib)
cc main.c -lclass
or (during linking)
ld ... main.o -lclass ...
is the same as:
cc main.c class1.o class2.o class3.o
- The Single UNIX® Specification, Issue 7 from The Open Group : create and maintain library archives – Commands & Utilities Reference,
- Linux User Commands Manual –
- FreeBSD General Commands Manual –
- Version 7 Unix Programmer's Manual –
- The 32-bit PA-RISC Run-time Architecture Document, HP-UX 11.0 Version 1.0, Hewlett-Packard, 1997.
See Chapter 4: Relocatable Libraries. Available at  (devresource.hp.com)
- "Static Libraries". TLDP. Retrieved 3 October 2013.
- "ar(1) - Linux man page". Retrieved 3 October 2013.
- Linux Standard Base Core Specification, version 4.1, Chapter 15. Commands and Utilities > ar
- John R. Levine (1999). Linkers and Loaders. Morgan–Kauffman., chapter 6: Libraries
- Manual page for NET/2 ar file format
- "ar.h". www.unix.com. The UNIX and Linux Forums.
- Manual page for NET/2 ranlib utility
- Manual page for NET/2 ranlib file format
- Pietrek, Matt (April 1998), "Under The Hood", Microsoft Systems Journal, retrieved 2014-08-23