From 68d5e54b427e3ccd5f1a73dcf8a7936d8b660fb8 Mon Sep 17 00:00:00 2001 From: Aki Date: Mon, 26 Jul 2021 00:49:06 +0200 Subject: Published ar guide --- how_to_archive_with_posix_ar.html | 106 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 how_to_archive_with_posix_ar.html (limited to 'how_to_archive_with_posix_ar.html') 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 @@ + + + + + + + + + +How To Archive With POSIX ar + + + +
+

How To Archive With POSIX ar

+

Published on 2021-07-26 00:47:00+02:00 +

Let's continue last POSIX archiving guide 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. + +

It's time to stop pretending that ar +does not exists and learn how to use it. + +

Now then, when exactly do we want to use ar? Luckily, that's simple to answer. ar is used to create +libraries of object files that are meant to be later linked against. In other words, it creates static libraries. +In order to do it, ar maintains a symbol table. As long as at least one of the archived files is an object file, +ar 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. + +

To create an empty archive: + +

+$ ar -r archive.a
+
+ +

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

+$ ar -r archive.a file_a.o file_b.o ../../file_c.o
+
+ +

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

To list contents of the symbol table, so the contents of the archive, use: + +

+$ ar -t archive.a
+file_a.o
+file_b.o
+file_c.o
+
+ +

Nice! Notice, that the file_c.o 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 archive.a by: + +

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

This will overwrite our current file_c.o 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.

+ +palm tree + +

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 -x option: + +

+$ ar -x archive.a
+
+ +

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: + +

+$ ar -x archive.a file_c.o
+$ cat file_c.o
+Aloha!
+
+ +

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

+$ ar -p archive.a file_c.o
+Aloha!
+
+ +

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

And that's about it regarding basic ar usage. Quite a few sources will probably recommend that you always run +-s option in every single operation you perform, in order to regenerate symbol table. However, that's not +required at all, ar 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. + +

If you are interested in precise management, then there are couple of options that will help i.a., -d +for removal, -u for restricted updates, or -p for unchecked appending. But again, in normal +usage, you probably won't need them as -r will deal with almost anything a Makefile will need. +

+ -- cgit v1.1