When our program has frequently used modules, and this module is also used in other programs, at this time according to the idea of software reuse, we should create libraries, so that later programming can reduce the amount of development code. This introduces command AR, which is used to manipulate the library.
1.ar Basic Usage
The AR command can be used to create, modify, or present a single module from a library. A library is a separate file that contains other files that are organized according to a specific structure (called the member of this library file). The content, mode, timestamp, owner, group, and other attributes of the original file are preserved in the library file.
The following is the format of the AR command:
AR [-]{DMPQRTX}[ABCFILNOPSSUVV] [membername] [count] archive files ...
For example, we can use ar rv libtest.a hello.o hello1.o .
Generate a library whose name is test, which can be linked with-ltest. The library contains two modules hello.o and HELLO1.O. You can have the '-' character before the option, or you can
No. Let's take a look at the command's action options and any options. Now we refer to the {DMPQRTX} section as the action option, while the [ABCFILNOPSSUVV] section is called an option.
The action options in {DMPQRTX} are only available in the command and must use one of them, meaning the following:
- D: Remove the module from the library. Specifies the module to be deleted by the original file name of the module. If any option V is used, each module that is deleted is listed.
- M: This action is to move a member in a library. When a library has several modules with the same symbolic definition (such as a function definition), the position 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 ' option to move to the specified position.
- P: Displays the members specified in the library to the standard output. If you specify any option V, the name of the member is displayed before the content of the member is output. If you do not specify the name of the member, all files in the library will be displayed.
- Q: Quick append. Add a new module to the end of the library. Does not check if replacement is required. The ' A ', ' B ', or ' I ' options have no effect on this operation, and the module is always appended at the end of the library. If any option V is used, each module is listed. At this point, the library's symbol table is not updated, you can use ' ar s ' or ranlib to update the library's symbol table index.
- R: Insert the module (replace) in the library. When the inserted module name already exists in the library, replace the module with the same name. If there is a module in several modules that 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 the added position.
- T: Displays a list of module tables for the library. Typically only the module name is displayed.
- X: Extracts a member from a library. If you do not specify a module to extract, all modules in the library are extracted.
Here's a look at any option that can be used in conjunction with the action options:
- A: Add a new file after an already existing member of the library. If you use either option A, you should specify a member name that already exists for the membername parameter in the command line.
- B: Add a new file in front of an already existing member of the library. If you use either option B, you should specify a member name that already exists for the membername parameter in the command line.
- C: Create a library. It is created regardless of whether the library exists.
- F: Truncate the specified name in the library. By default, the file name length is unrestricted, and you can use this parameter to truncate the file name to ensure compatibility with other systems.
- I: Add a new file in front of an already existing member of the library. If you use either option I, you should specify a member name that already exists for the membername parameter in the command line (similar to option B).
- L: not currently used
- N: Used with the count parameter to specify the number of extracts or outputs when there are multiple identical filenames in the library.
- O: Retains the original data of the member when the member is extracted. If you do not specify this option, the time of the extracted module is marked as the extracted time.
- P: Use full pathname when file name matches. AR cannot use the full pathname when creating the library (such a library file is not POSIX compliant), but some tools can.
- S: Writes a target file index into the library, or updates an existing target file index. This action is even done for libraries that do not have any changes. Doing AR s for a library is equivalent to doing ranlib to the library.
- S: Do not create a target file index, which can speed up time when creating large libraries.
- U: Generally speaking, command ar R ... Insert all listed files into the library, and you can use this option if you want to insert only those files that are listed in the file that are newer than the library file name. This option is available only for the R operation option.
- V: This option displays additional information for the option to perform the operation.
- V: Displays the version of AR.
Linux ar command