ar (Unix)

From Wikipedia, the free encyclopedia

Jump to: navigation, search


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. ar is included as one of the GNU Binutils.

For example, 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

to compile a program that depends on class1.o, class2.o, and class3.o one could do:

cc main.c libclass.a

instead of:

cc main.c class1.o class2.o class3.o 

While it is technically possible to use the -L. switch and -lclass to achieve the same result in the linking phase, it is considered bad programming. As a rule of thumb, the -l should only be used for installed libraries.

Contents

The ar format has never been standardised; modern archives are based on a common format with two known variants, BSD and GNU.

Historically there have been other variants including AIX (small), AIX (big) and Coherent; they vary significantly from the common format.

Debian ".deb" archives use the common format.

An ar file begins with a global header, follow by a header and data section for each file stored within the ar file.

The data section is 2 byte aligned, if it would end on an odd offset a "\n" is used as filler.

The global header is a single field containing the magic ASCII string "!" followed by a single LF control character

The common format is as follows.

Field Offset from Field Offset to Field Name Field Format
0 15 File name ASCII
16 27 File modification timestamp Decimal
28 33 Owner ID Decimal
34 39 Group ID Decimal
40 47 File mode Octal
48 57 File size in bytes Decimal
58 59 File magic \140\012

Due to the limitations of file name length and format, both the GNU and BSD variants devised different methods of achieving extended filenames.

BSD ar store extended filenames by placing the string "#1/" followed by the file name length in the file name field, and appending the real filename to the file header.

GNU ar 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.

GNU ar uses a '/' to mark the end of the filename, this allows for the use of spaces without the use of an extended filename.

GNU 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 consists of the following fields:

Field Format Field Description
32-bit big endian integer Number of entries in the symbol table
32-bit big endian integers One integer for each symbol, recording the position within the archive of the header for the file containing this symbol.
Zero-terminated string Consumes the rest of the "/" special file. Each symbol name is zero-terminated, and are in the same order as the previous set of integers.

Advanced Search
Included Web Search Engines


Safe Search

close

Top Matching Results

Occasionally Search.com will highlight specialized results that are based on the context of your query. Examples of specialized results include specific links to news, images, or video.

Top Matching Results may highlight information from other Search.com pages, content from the CNET Network of sites, or third party content. The listings are based purely on relevance. Search.com does not receive payment for listings in this section but our partners that provide this data may get paid for listing these products.

Sponsored Links

This section contains paid listings which have been purchased by companies that want to have their sites appear for specific search terms and related content. These listings are administered, sorted and maintained by a third party and are not endorsed by Search.com.

Search Results

Search.com sends your search query to several search engines at one time and integrates the results into one list which has been sorted by relevance using Search.com's proprietary algorithm. You can customize the list of search engines included in your metasearch from the preferences.

The search engines that are used in your metasearch may allow companies to pay to have their Web sites included within the results. To view the Paid Inclusion policy for a specific search engine, please visit their Web site. Search.com does not accept payment or share revenue with any search engine partner for listings in this section.