Using GNU Binary Utilities227
Let us create an archive of simple text files. The following command creates an archive
test.a
from files
/etc/hosts
and
/etc/resolv.conf
.
ar -r test.a /etc/resolv.conf /etc/hosts
This command creates a file
test.a
that contains the contents of the other two files. To
display the contents of the file, you can use the –
t
option on the command line.
[root@boota ar]# ar -t test.a
resolv.conf
hosts
[root@boota ar]#
Using –
x
option, you can extract one or more files from the archive. The following com-
mand extracts the file
hosts
from the archive.
ar -x test.a hosts
Now let us create a real library file from two object files
common.o
and
ftp.o
. The fol-
lowing command creates a file
libcommon.a
from these two object files.
ar -r libcommon.a common.o ftp.o
Functions or variables in these two files can be linked to any other object file using the –
l
command line option with
gcc
compiler. The following command line creates an executable file
project
from this library and
project.o
file.
gcc project.o –lcommon –o project
Note that
libcommon.a
is not used with the –
l
option when linking functions in this
library to an executable file. Instead we used
common
as the library name by stripping the lead-
ing
lib
part and
.a
trailing part.
Files can be inserted into the archive in a particular order. Similarly you can also use a pol-
icy when replacing existing files using different command line options. Other options that can be
used with the
ar
command can be listed by executing the command without an argument as
shown below.
[root@boota /root]# ar
Usage: ar [-X32_64] [-]{dmpqrstx}[abcfilNoPsSuvV] [member-
name] [count] archive-file file...
ar -M [
commands:
d - delete file(s) from the archive
m[ab] - move file(s) in the archive
p - print file(s) found in the archive
q[f] - quick append file(s) to the archive
r[ab][f][u] - replace existing or insert new file(s) into
the archive
t - display contents of archive
x[o] - extract file(s) from the archive