When we have frequently used modules in our program, and such modules are also used in other programs, then according to the idea of software reuse, we should generate them into libraries, so that we can reduce the amount of development code in the future programming. The ar command is used to manipulate libraries.

1. Basic usage of AR

The ar command can be used to create, modify, and extract individual modules from libraries. A library is a single file that contains other files organized according to a specific structure (called members of the library file). The content, mode, timestamp, owner, group, and other attributes of the original file are retained in the library file.

The ar command format is as follows: ar [-]{DMPQRTX}[abcfilNoPsSuvV] [membername] [count] Archive files…

For example, we can use ar rv libtest.a hello.o hello1.o to generate a library named test, and link with -ltest.

The library houses two modules hello.o and hello1.o.

Options may or may not be preceded by a ‘-‘ character.

Let’s look at the action options and options of the command. Now we call the {DMPQRTX} part the action option, and the [abcfilNoPsSuvV] part the optional.

The action options in {DMPQRTX} can and must be used in only one of the commands, and their meanings are as follows:

  • -d: Removes modules from the library. Specify the module to be deleted by its original file name. If option V is used, each module removed is listed.
  • -m: Moves members in a library. When several modules in a library have the same symbolic definition (such as a function definition), the placement order of the members is important. If no option is specified, any specified member is moved to the end of the library. You can also use the ‘A’, ‘b’, or ‘I’ options to move to a specified location.
  • -p: Displays the specified member in the library to standard output. If you specify option v, the name of the member is displayed before the content of the member is printed. If no member name is specified, all files in the library are displayed.
  • -q: Quick append. Add a new module to the end of the library. It does not check whether a replacement is required. The ‘A’, ‘b’, or ‘I’ options have no effect on this operation, the module is always at the end of the appended library. Each module is listed if option V is used. At this point, the library’s symbol table is not updated. You can update the library’s symbol table index with ‘ar s’ or ranlib.
  • -r: Inserts a module (replace) in the library. When the inserted module name already exists in the library, the module with the same name is replaced. If one of several modules does not exist in the library, ar displays an error message and does not replace other modules with the same name. By default, new members are added at the end of the library, and other options can be used to change where they are added.
  • -t: Displays the module list of the library. Generally, only the module name is displayed.
  • -x: Retrieves a member from the library. If the module to extract is not specified, all modules in the library are extracted.

Let’s look at some of the options that can be used in conjunction with action options:

  • -a: Adds a new file after an existing member of the library. If you use either option a, you should specify an existing membername for the membername parameter on the command line.
  • -b: Adds a new file to an existing member of the library. If option b is used, an existing membername should be specified for the membername parameter on the command line.
  • -c: Create a library. Will be created regardless of whether the library exists.
  • -f: truncates the specified name in the library. By default, the length of a file name is unlimited. You can use this parameter to shorten the file name to ensure compatibility with other systems.
  • -i: Adds a new file to an existing member of the library. If you use option I, you should specify an existing membername for the membername parameter on the command line (similar to option b).
  • -l: Not in use
  • -N: Used with the count argument to specify the number of extracts or outputs if there are more than one of the same file names in the library.
  • -o: When extracting a member, retain the original data of the member. If this option is not specified, the time of the extracted module is marked as the extracted time.
  • -P: The full path name is used for file name matching. Ar cannot use full pathnames when creating libraries (such library files do not comply with POSIX standards), but some tools can.
  • -s: Writes an object file index to the library, or updates an existing object file index. Do this even for libraries that do not have any changes. Doing AR s to a library is equivalent to ranlib to that library.
  • -S: Does not create object file indexes, which can speed up the creation of larger libraries.
  • -u: Generally speaking, the command ar r… Insert all listed files into the library. Use this option if you only want to insert files in the list file that are newer than those in the library with the same name. This option is used only for the R action option.
  • -v: This option displays additional information about the option to perform the action.
  • -V: Displays the AR version.