Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Linux basic command-ar

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

Ar

The ar directive can create, modify, or extract individual modules from the library. A library is a separate file that contains other files organized according to a specific structure, which we call member. The archive file is usually a binary file, and we generally use the archive file as a library. The contents, mode (permissions), timestamp, owner, and group of the original file are saved in the archive and can be restored when extracted.

GNU ar can maintain files of its members with names of any length; however, depending on how ar is configured on your system, you can limit the length of member names to be compatible with the archive format maintained by other tools. If there is, the limit is usually 15 characters (typical a.out-related format) or 16 characters (typical coff-related format).

When you specify a modifier, ar creates an index on the symbols defined in the retargetable object module in the archive. After creation, the index is updated in the archive (except for the Q update operation) whenever ar makes changes to its contents. An archive with such an index can speed up linking to the library and allow routines in the library to call each other, regardless of their location in the archive. You can use "nm-s" or "nm-- print-armap" to list this index table. If tables are missing from the archive, you can add tables using another form of ar called ranlib.

GNU ar is free to create a thin archive that contains a symbolic index and a reference to the original copy of the file member file. Such archives are useful for building libraries for local builds, because in a local build, relocatable objects remain available, while copying the contents of each object is a waste of time and space. Thin files are also flat, so adding one or more files to a thin archive will add nested archive elements separately. The path of the archive element is stored relative to the archive itself.

GNU Ar is designed to be compatible with two different facilities. You can use command-line options to control its activity, such as different types of ar; on Unix systems, or, if you specify a single command-line option-M, you can control it using scripts provided with standard input, such as the MRI "librarian" program.

The scope of this command: RedHat, RHEL, Ubuntu, CentOS, SUSE, openSUSE, Fedora.

1. Grammar

Ar [--plugin name] [- X32 relpos 64] [-] p [mod [relpos] [count]] archive [member...]

2. List of options

GNU ar allows you to mix the opcode p and the modifier flag mod in any order in the first command-line argument. If you prefer, you can start the first command line argument with "-". The p key letter specifies the action to be performed; it can be any of the following, but you must specify only one of the actions

Operation code

Description

C

Create an archive file

D

Delete the member file in the archive and specify the name of the module to be deleted as member. If you do not specify a file to delete, the archive remains unchanged

M

Change the order of member files in the archive. If a symbol is defined among multiple members, the sorting of members in the archive changes the way the program uses library links. If no modifiers are used in "m", any members named in the member parameters are moved to the end of the archive file; you can use the a, b, or I modifiers to move them to the specified location.

P

Prints the specified member of the archive to a standard output file. If the v modifier is specified, display the member name before copying its contents to standard output.

Q

Append the file to the end of the archive. The modifiers a, b, and I do not affect this operation; new members are always placed at the end of the archive. The modifier v causes each file to have an ar list when appended. Because the focus of this operation is speed, the archived symbol table index is not updated, even if it already exists; you can explicitly use ar or ranlib to update the symbol table index. However, there are too many different systems that assume that indexes are appended quickly, so GNU ar implements Q as a synonym for r.

R

Insert the file 'member' into the archive, and if there is a file to insert in the archive, overwrite it. The difference between this action and Q is that if the names of existing members match the members you want to add, delete them. If there is no 'member',ar among the members, an error message is displayed and any existing archive members that match that name are kept undisturbed. By default, a new member is added at the end of the file; however, you can use one of the modifiers a, b, or I to request the position relative to the existing member. The modifier v used with this operation will trigger a line of output for each file inserted. The letter an or r to indicate whether the file is appended (old members are not deleted) or replaced.

T

Displays a list of files contained in the archive file. Usually only the member name is displayed; if you also want to view the mode (permissions), timestamp, owner, group, and size, you can also request it by specifying the v modifier. If no member is specified, all files in the archive are listed. If there are multiple files with the same name in an archive, only the first instance is listed; to view them, you must require a complete list.

X

You can use the v modifier to extract member files from the archive file and ask ar to list each name at extraction time. If you do not specify a member, all files in the archive are extracted. Unable to extract files from the thin archive.

Many modifiers (Mod) can be followed by the p key to specify changes in operation behavior

Modifier

Description

A

Add a new file after the existing member of the archive. If you use the modifier a, the name of the existing archive member must appear before the archive specification as a relpos parameter.

B

Add a new file before the existing member of the archive. If you use the modifier b, the name of the existing archive member must appear before the archive specification as a relpos parameter. (same as I)

C

Create an archive. When you request an update, if the specified archive does not exist, it is always created. However, unless you specify in advance to create a warning by using this modifier, a warning will be issued.

D

Operate in deterministic mode. When adding files and archive indexes, use zeros for UID, GID, and timestamps, and use a consistent file mode for all files. When using this option, if ar is used with the same option and the same input file, multiple runs will create the same output file, regardless of the owner, group, file mode, or modification time of the input file.

F

Truncate the name in the archive. GNU ar usually allows a file name of any length. This will cause it to create files that are not compatible with native ar programs on some systems. If this is a problem, you can use the f modifier to truncate the file name when placing the f modifier in the archive.

I

Insert a new file before an existing member of the archive. If you use the modifier I, the name of the existing archive member must appear before the archive specification as a relpos parameter. (same as b)

L

Useless modifiers

N

Use the count parameter. Use this method if there are multiple entries with the same name in the archive. Extract or delete the number of instances with a given name from the archive

O

The original date of the member is retained when the member is extracted. If you do not specify this modifier, the file extracted from the file will override the extraction time.

P

Use the full path name when matching names in the archive. GNU ar cannot create an archive with a full pathname (this type of archive is not a POSIX complaint), but other archive creators can. This option causes GNU ar to match the file name with the full pathname, which is convenient when extracting a single file from an archive created by another tool.

S

Write the object file index to the archive, or update the existing index, even if no other changes have been made to the archive. You can use this modifier flag in any operation, or you can use it alone. Running ar on an archive is equal to running ranlib on it.

S

Do not generate archived symbol tables. This can speed up several steps in building a large library. The result archive cannot be used with the linker. In order to build the symbol table, the S modifier must be omitted during the last execution of ar, or ranlib must be run on the archive.

T

Sets the specified archive file to a thin archive. If it already exists and is a regular archive, the existing members must be the same as the archive directory.

U

Typically, ar r inserts all the files listed into the archive. Use this modifier if you only want to insert files that are newer than existing members with the same name. The u modifier is only allowed to operate on r (replace). In particular, the use of combined qu is not allowed because checking the timestamp loses any speed advantage of operation Q.

V

Show detailed execution process

V

Display the version of ar

AR ignores the initial option-x32Secret64 to be compatible with AIX. The behavior generated by this option is the default behavior of GNU ar. Ar does not support any other-X option; in particular, it does not support-x32, which is the default setting for AIX ar.

The optional command line switch "--plugin name" causes ar to load a plug-in named name, which adds support for more file formats. This option is available only if plug-in support is enabled in the tool chain.

The @ file option, which reads the command line options from the file. The read option is inserted to replace the original @ file option. If the file does not exist or cannot be read, the option is processed literally without deleting it. Options in file are separated by spaces. You can include space characters in options by enclosing the entire option in single or double quotation marks. Any character, including the backslash, can be included in the backslash by the characters contained in the prefix. File itself may contain additional @ file options; any such options will be processed recursively.

3. Examples

1) create an archive file

[root@localhost weijie] # ar-rc mlib.a 1.c 2.c / / create an archive

[root@localhost weijie] # ls

11.c 1.c 1.c ~ 2.c 3.c 4.c 4.c ~ 5.c 6.c mlib.a

[root@localhost weijie] # ar-t mlib.a / / View the list of archived files

1.c

2.c

2) insert a file in the archive

[root@localhost weijie] # ar-rc mlib.a 3.c / / insert 3.c, which is not available in the archive at this time

[root@localhost weijie] # ar-t mlib.a / / View file list, 3.c was added

1.c

2.c

3.c

[root@localhost weijie] # ar-rc mlib.a 1.c / / insert 1.c. Note that 1.c already exists.

[root@localhost weijie] # ar-t mlib.a / / View file list, 1.c has been overwritten

1.c

2.c

3.c

3) insert the file at the end of the archive

[root@localhost weijie] # ar-Q mlib.a 1.c / / is appended to the end with option Q, note that this is different from r

[root@localhost weijie] # ar-t mlib.a / / View file list, adding 1.c

1.c

2.c

3.c

1.c

4) remove files from the archive

[root@localhost weijie] # ar-d mlib.a 1.c / / Delete 1.c

[root@localhost weijie] # ar-t mlib.a / / View the list of files. The first 1.c has been deleted, but there is one at the end.

2.c

3.c

1.c

[root@localhost weijie] # ar-d mlib.a 1.c / / Delete 1 again. C

[root@localhost weijie] # ar-t mlib.a / / View file list, 1.c has been deleted

2.c

3.c

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report