summaryrefslogtreecommitdiff
path: root/how_to_archive_with_posix_ar.html
blob: 19fc08272356e7470c50a21921dbbeeba8777663 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<!doctype html>
<html lang="en">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="author" content="aki">
<meta name="tags" content="tutorial, posix, ar, archive">
<meta name="published-on" content="2021-07-26T00:47:00+02:00">
<meta name="last-modified-on" content="2021-07-26T17:09:00+02:00">
<link rel="icon" type="image/png" href="favicon.png">
<link rel="stylesheet" href="style.css">

<title>How to Archive With POSIX ar</title>

<nav><p><a href="https://ignore.pl">ignore.pl</a></p></nav>

<article>
<h1>How to Archive With POSIX ar</h1>
<p class="subtitle">Published on 2021-07-26 00:47:00+02:00, last modified on 2021-07-26 17:09:00+02:00
<p>Let's continue <a href="how_to_archive_with_posix_tar_cpio_and_pax.html">last POSIX archiving guide</a> from the
very spot we finished. Today, we'll deal with just one tool. Very peculiar one, because it's more of a development
utility than an archiver with portable format. In fact, it doesn't have a defined format at all. More than that, POSIX
acknowledges that several incompatible formats are known and that they don't care. User can usually count on one
implementation to support archives created on another machine using the same implementation, so it's not that bad.

<p>It's time to stop pretending that <a href="https://pubs.opengroup.org/onlinepubs/9699919799/utilities/ar.html">ar</a>
does not exist and learn how to use it.

<p>Now then, when exactly do we want to use <b>ar</b>? Luckily, that's simple to answer. <b>ar</b> is used to create
libraries of object files that are meant to be later linked against. In other words, it creates <i>static libraries</i>.
In order to do it, <b>ar</b> maintains a symbol table. As long as at least one of the archived files is an object file,
<b>ar</b> is obliged to do it completely automatically. However, usability of the archive for the linker is only
guaranteed if all of the files are objects.

<p>To create an empty archive:

<pre>
$ ar -r archive.a
</pre>

<p>Now, that's not very useful. Luckily for us, option <code>-r</code> actually means "replace or add" and accepts any
number of paths after the archive name:

<pre>
$ ar -r archive.a file_a.o file_b.o ../../file_c.o
</pre>

<p>Assuming, that all of these files are present, they'll all be added to a new archive. If the <i>archive.a</i> already
happens to exist, then files that are already occurring in it will get replaced. If not, then files will get appended.

<p>To list contents of the symbol table, so the contents of the archive, use:

<pre>
$ ar -t archive.a
file_a.o
file_b.o
file_c.o
</pre>

<p>Nice! Notice, that the <i>file_c.o</i> was added directly, without any additional path - just basename of the file.
This will happen to every single file added to the archive. While adding files with same names you need to be cautious
about it. Consider modifying our <i>archive.a</i> by:

<pre>
$ echo "Aloha!" > ../file_c.o
$ ar -r archive.a ../file_c.o
</pre>

<p>This will overwrite our current <i>file_c.o</i> with one that says "Aloha!". This could be problematic, but it's
generally easy to avoid and there are additional options that allow to do some shenanigans with the archive. Refer to
your friendly manual or standard itself.</p>

<img src="how_to_archive_with_posix_ar-1.png" alt="palm tree">

<p>You probably won't need to extract files from the archive, simply because you put it together so that the linker uses
it and not yourself, usually in a variation of one of the following ways. All depending on what tool is used as linker
and if the archive is available to it through its search paths:

<pre>
$ cc -l:archive.a main.o -o main
$ ld main.o archive.a -o main
</pre>

<p>Still, if you need to extract files, you can extract them from library in two ways. First one uses <code>-x</code>
option:

<pre>
$ ar -x archive.a
</pre>

<p>This simply extracts all of the files from the archive directly into current directory. Keep in mind that the paths
are not remembered, only the basenames are. Using those basenames you can also extract selected files from the archive:

<pre>
$ ar -x archive.a file_c.o
$ cat file_c.o
Aloha!
</pre>

<p>The other way of extracting is closer to <b>cpio</b>. By using <code>-p</code> option content of selected files or
the entire archive will be printed out to standard output:

<pre>
$ ar -p archive.a file_c.o
Aloha!
</pre>

<p>Just note that if you specify multiple files to output, they will get concatenated.

<p>And that's about it regarding basic <b>ar</b> usage. Quite a few sources will probably recommend that you always run
<code>-s</code> option in every single operation you perform, in order to regenerate symbol table. However, that's not
required at all, <b>ar</b> should be able to deal with it without any input from the user. The only case in which it is
actually viable is after the archive was stripped. Otherwise, this suggestion is almost always useless.

<p>If you are interested in precise management, then there are couple of options that will help i.a., <code>-d</code>
for removal, <code>-u</code> for restricted updates, or <code>-p</code> for unchecked appending. But again, in normal
usage, you probably won't need them as <code>-r</code> will deal with almost anything a <i>Makefile</i> will need.
</article>
<script src="https://stats.ignore.pl/track.js"></script>