summaryrefslogtreecommitdiff
path: root/how_to_archive_with_posix_ar.html
diff options
context:
space:
mode:
authorAki <please@ignore.pl>2021-07-26 00:49:06 +0200
committerAki <please@ignore.pl>2021-07-26 00:50:07 +0200
commit68d5e54b427e3ccd5f1a73dcf8a7936d8b660fb8 (patch)
tree636c9f789e7901849712be088fdfd074f1e71d7f /how_to_archive_with_posix_ar.html
parent8e1f3c9ebc0ccd132e3836f3d198415a15932877 (diff)
downloadignore.pl-68d5e54b427e3ccd5f1a73dcf8a7936d8b660fb8.zip
ignore.pl-68d5e54b427e3ccd5f1a73dcf8a7936d8b660fb8.tar.gz
ignore.pl-68d5e54b427e3ccd5f1a73dcf8a7936d8b660fb8.tar.bz2
Published ar guide
Diffstat (limited to 'how_to_archive_with_posix_ar.html')
-rw-r--r--how_to_archive_with_posix_ar.html106
1 files changed, 106 insertions, 0 deletions
diff --git a/how_to_archive_with_posix_ar.html b/how_to_archive_with_posix_ar.html
new file mode 100644
index 0000000..b4dce3b
--- /dev/null
+++ b/how_to_archive_with_posix_ar.html
@@ -0,0 +1,106 @@
+<!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">
+<link rel="icon" type="image/png" href="cylo.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
+<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.
+
+<p>It's time to stop pretending that <a href="https://pubs.opengroup.org/onlinepubs/9699919799/utilities/ar.html">ar</a>
+does not exists 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. Still, you can extract files from it in two ways. First one is <code>-x</code> option:
+
+<pre>
+$ ar -x archive.a
+</pre>
+
+<p>This simply extract 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>