tags:

views:

207

answers:

6

I don't really understand what ar utility does on Unix systems.

I know it can be somehow used for creating c libraries, but all that man page tells me is that it is used to make archives from files, which sounds similar to, for example, tar....

+2  A: 

Exactly, ar is an archiver. It simply takes a set of object files (*.o) and put them in an archive that you call a static library.

yves Baumes
why don't use tar, then?
Karel Bílek
historical reasons, mostly.
lfaraone
...because `tar` was used for storing files to **tape** (hence the 't'). It's no use having your libraries on a tape - you'd have to wait for the operator to mount the correct volume, just to link your program.
alex tingle
oh, thanks. thats probably it.
Karel Bílek
+8  A: 

The primary purpose is to take individual object files (*.o) and bundle them together into a static library file (*.a). The .a file contains an index that allows the linker to quickly locate symbols in the library.

Tar doesn't create files that linkers understand.

Brian Neal
The causality kinda goes the other direction: the original unix linker was written to use the "ar" utility to store its libraries. It was only later that the "tar" utility became popular, largely because of better interoperability with tape devices.
Andy Ross
+1  A: 

It takes code in the form of object files (.obj, .o, etc) and makes a static library (archive). The library can then be included when linking with ld to include the object code into your executable.

Take a look at the example usage in the Wikipedia article.

progrmr
+1  A: 

You might want to run man ar to get the full picture. Here's a copy of that on the web.

To quote:

The GNU ar program creates, modifies, and extracts from archives. An archive is a single file holding a collection of other files in a structure that makes it possible to retrieve the original individual files (called members of the archive).

ar is considered a binary utility because archives of this sort are most often used as libraries holding commonly needed subroutines.

lfaraone
+3  A: 

ar is a general purpose archiver, just like tar. It just "happens" to be used mostly for creating static library archives, one of its traditional uses, but you can still use it for general purpose archiving, though tar would probably be a better choice. ar is also used for Debian .deb packages.

Paggas
+1  A: 

ar is specifically for archives (or libraries) of object code; tar is for archives of arbitrary files. Anybody's guess why GNU refers to these as 'archives', in other environments this utility is called the 'librarian', and the resulting files just libraries.

Clifford