What does the "rcs" option in ar do?

27,310

Solution 1

Reading the manual page (for ar) is a good start:

c

Create the archive. The specified archive is always created if it did not exist, when you request an update. But a warning is issued unless you specify in advance that you expect to create it, by using this modifier.

r

Insert the files member... into archive (with replacement). This operation differs from q in that any previously existing members are deleted if their names match those being added.

s

Write an object-file index into the archive, or update an existing one, even if no other change is made to the archive. You may use this modifier flag either with any operation, or alone. Running "ar s" on an archive is equivalent to running ranlib on it.

Comparing with POSIX, you may notice one difference: GNU ar makes the "-" prefixing options optional in itself.

An archive can contain other items than object-files (though this is not done often). Archives containing object-files require additional maintenance (e.g., done by ranlib) to make them usable by the linker.

According to the Rationale in POSIX ar, the -s option originated in BSD (System V did this automatically). However, running ranlib by itself is the prevailing practice (-s is rarely used). Interestingly enough, POSIX does not have ranlib, and ultimately the -s option will replace ranlib in the multitude of makefiles which were written to run on a variety of platforms.

Solution 2

Reading the manual for ar helps but I will explain it in more detail. ar -rcs is the most likely command you would use when using a Makefile to compile a library. r means that if the library already exists, replace the old files within the library with your new files. c means create the library if it did not exist. s can be seen to mean 'sort' (create a sorted index of) the library, so that it will be indexed and faster to access the functions in the library. Therefore, rcs can be seen to mean replace, create, sort.

Solution 3

This means "Insert the files member... into archive (with replacement)."

Share:
27,310
Admin
Author by

Admin

Updated on August 26, 2021

Comments

  • Admin
    Admin almost 3 years

    I did read the man file but it does not help. rcs seems to be the most popular option to pass to ar, but the meaning isn't so clear to me.

    So c means to create a new archive, but then why use r? which seems to stand for "replace"? What will the s option do to the output?