-1

Many articales provide examples of using "ar" command uses "ar rcs " without explaination of the reason.

I've tried the command by myself both with and without modifier "c" and "s" and find that the output are idendical even in binary comparasion.

Run "ar" without modifier "c" and "s":

$ ar r out.a *.txt
ar: creating out.a
$ hexdump -C out.a
00000000  21 3c 61 72 63 68 3e 0a  61 2e 74 78 74 2f 20 20  |!<arch>.a.txt/  |
00000010  20 20 20 20 20 20 20 20  31 35 37 37 33 35 33 37  |        15773537|
00000020  35 38 20 20 31 30 30 30  20 20 31 30 30 30 20 20  |58  1000  1000  |
00000030  31 30 30 36 36 34 20 20  34 20 20 20 20 20 20 20  |100664  4       |
00000040  20 20 60 0a 61 62 63 0a  62 2e 74 78 74 2f 20 20  |  `.abc.b.txt/  |
00000050  20 20 20 20 20 20 20 20  31 35 37 37 33 35 33 37  |        15773537|
00000060  35 38 20 20 31 30 30 30  20 20 31 30 30 30 20 20  |58  1000  1000  |
00000070  31 30 30 36 36 34 20 20  34 20 20 20 20 20 20 20  |100664  4       |
00000080  20 20 60 0a 64 65 66 0a                           |  `.def.|
00000088
$ rm out.a

Run "ar" with modifier "c" and "s":

$ ar rcs out.a *.txt
$ hexdump -C out.a
00000000  21 3c 61 72 63 68 3e 0a  61 2e 74 78 74 2f 20 20  |!<arch>.a.txt/  |
00000010  20 20 20 20 20 20 20 20  31 35 37 37 33 35 33 37  |        15773537|
00000020  35 38 20 20 31 30 30 30  20 20 31 30 30 30 20 20  |58  1000  1000  |
00000030  31 30 30 36 36 34 20 20  34 20 20 20 20 20 20 20  |100664  4       |
00000040  20 20 60 0a 61 62 63 0a  62 2e 74 78 74 2f 20 20  |  `.abc.b.txt/  |
00000050  20 20 20 20 20 20 20 20  31 35 37 37 33 35 33 37  |        15773537|
00000060  35 38 20 20 31 30 30 30  20 20 31 30 30 30 20 20  |58  1000  1000  |
00000070  31 30 30 36 36 34 20 20  34 20 20 20 20 20 20 20  |100664  4       |
00000080  20 20 60 0a 64 65 66 0a                           |  `.def.|
00000088
$

So anybody can give an example to show the difference between adding and not adding the modifier?

I've read the manual page but still can't get the point.


Added 2019-12-27 02:28:45 UTC

Thank you. Now I've understand modifier "c", but still have questions on modifier "s".

Many have mentioned "symbol", but what does it mean?

I've tried symbol link, but they are still identical:

[john@centos8-01 arTest]$ ls -l
total 8
-rw-r--r--. 1 john smith 4 Dec 27 10:18 a.txt
-rw-r--r--. 1 john smith 4 Dec 27 10:19 b.txt
lrwxrwxrwx. 1 john smith 5 Dec 27 10:19 c.txt -> a.txt
[john@centos8-01 arTest]$ ar -r out1.a *.txt
ar: creating out1.a
[john@centos8-01 arTest]$ ar -rs out2.a *.txt
ar: creating out2.a
[john@centos8-01 arTest]$ diff out1.a out2.a
[john@centos8-01 arTest]$ cat out1.a
!<arch>
a.txt/          1577413136  1001  1001  100644  4         `
abc
b.txt/          1577413141  1001  1001  100644  4         `
def
c.txt/          1577413136  1001  1001  100644  4         `
abc
[john@centos8-01 arTest]$ cat out2.a
!<arch>
a.txt/          1577413136  1001  1001  100644  4         `
abc
b.txt/          1577413141  1001  1001  100644  4         `
def
c.txt/          1577413136  1001  1001  100644  4         `
abc
[john@centos8-01 arTest]$

And I've tried adding symbols to my txt files:

[john@centos8-01 arTest]$ cat a.txt
abc
!@#$%^&*()_+-=~`[]\{}|;':"<>?,./
[john@centos8-01 arTest]$

As you can see "a.txt" had no symbols before but letters "abc" only, but now I've added all the symbols I can find on my keyboard. Now I run "ar" with and without modifier "s", but they are still identical:

[john@centos8-01 arTest]$ ls
a.txt  b.txt
[john@centos8-01 arTest]$ ar r out1.a *.txt
ar: creating out1.a
[john@centos8-01 arTest]$ ar rs out2.a *.txt
ar: creating out2.a
[john@centos8-01 arTest]$ diff out1.a out2.a

And there is no "symbol table":

[john@centos8-01 arTest]$ cat out1.a
!<arch>
a.txt/          1577413538  1001  1001  100644  37        `
abc
!@#$%^&*()_+-=~`[]\{}|;':"<>?,./

b.txt/          1577413141  1001  1001  100644  4         `
def
[john@centos8-01 arTest]$ cat out2.a
!<arch>
a.txt/          1577413538  1001  1001  100644  37        `
abc
!@#$%^&*()_+-=~`[]\{}|;':"<>?,./

b.txt/          1577413141  1001  1001  100644  4         `
def
[john@centos8-01 arTest]$
Vespene Gas
  • 163
  • 1
  • 6
  • afaik, the `s` flag tells it to write a symbol table. What symbols do you have in your text files? Also, I think that `ar` will write a symbol table by default (when it has any symbols to put into it). –  Dec 26 '19 at 10:47
  • yes, GNU ar will write a symbol table by default [now](https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;a=blob;f=binutils/ar.c;h=b8342ce65a1b530cff68e3287e706ba13988b593;hb=8ce18f9cdf53c846e0486130a66ba55c96fc2b14#l93). So the `s` flag is kind of a no-op. –  Dec 26 '19 at 10:51
  • And the `c` flag suppresses the warning about creating the archive, as your example already shows. –  Dec 26 '19 at 10:55
  • answered in the other forum [here](https://stackoverflow.com/questions/29714300/what-does-the-rcs-option-in-ar-do/29716788#29716788) – Thomas Dickey Dec 26 '19 at 12:58
  • IIRC, BSD did not need `-s` since the linker did check libraries more than once. AT&T UNIX needed this option to create a symbol table to make the same linker command work with the simpler AT&T linker. Later, BSD got `ranlib` and this was still a win, since it made linking faster. – schily Dec 26 '19 at 14:56
  • @mosvy so what are symbols and symbol table? I've updated my question and included what I tried (both symbol links and symbols on my keyboard), but still can't get the point. Can you please provide an answer? – Vespene Gas Dec 27 '19 at 02:43
  • @schily So what is a Symbol table? is there any example? I've changed my question and included what I tried as per my understanding of "symbols". – Vespene Gas Dec 27 '19 at 02:51

2 Answers2

2

The ar archives are generally used for object files, even if they could be misused for other purposes (the .deb files from debian are basically ar archives, for example).

If you compile a small C program like:

void foo(){}

into a foo.o object file, than that object file will contain a foo symbol which points to the compiled code making up the foo function. When you add such a file to an archive with ar rcs foo.a foo.o, that symbol will be added to a symbol index table, which is used by ld an other programs to quickly find which file from the archive contains a particular symbol.

Building that symbol table used to be optional, dependent on the s switch, but it's now the default with GNU ar, and the s switch is basically a no-op. Using the S (uppercase s) switch will prevent such an index from being built.

Example:

$ cc -Wall -c -o foo.o -xc - <<<'void foo(){}'
$ ar rc foo1.a foo.o
$ ar rcS foo2.a foo.o
$ cmp foo1.a foo2.a
foo1.a foo2.a differ: byte 9, line 2
$ nm -s foo1.a

Archive index:
foo in foo.o

foo.o:
0000000000000000 T foo
1

The -s option will (re-)create a symbol table in the archive. This could be useful if one does not already exist, or if it has been stripped with strip. Your archive contains no symbols to create a table for.

The -c option simply inhibits the output ar: creating out.a that you get when you don't use -c.

Kusalananda
  • 320,670
  • 36
  • 633
  • 936
  • So what is a "symbol"? Can you please give an example? I've updated my question, I've just tried both symbol link and adding all the symbols I can find on my keyboard to my txt file, but nothing changed. – Vespene Gas Dec 27 '19 at 02:41
  • @ColumbiaRover Any name that a compiler produces when compiling a C program (for example) is a symbol, i.e. a function name or a static variable. `ar` is then used to create a static library with a symbol table. This library can later be linked in with other libraries to create an executable file. – Kusalananda Dec 27 '19 at 06:50