ar (创建,修改和提取档案文件)

rose1 发表于 2020-08-07 10:14
浏览次数:
在手机上阅读

在类似Unix的操作系统上,ar命令创建,修改和提取档案文件。 本文档介绍了GNU / Linux版本的arch。

查看英文版

目录

1 ar 运行系统环境

2 ar 说明

3 ar 语法

4 ar 例子

ar 运行系统环境

Linux

ar 说明

ar程序创建,修改和从档案中提取。归档文件是一个单个文件,该文件在结构上可容纳其他文件的集合,从而可以检索原始的单个文件(称为归档文件的成员)。

原始文件的内容,模式(权限),时间戳,所有者和组保留在存档中,并可在提取时恢复。

GNU ar可以维护其成员具有任意长度名称的档案。但是,根据与系统上ar的配置方式,可能会限制文件名长度,以与其他工具使用的存档格式兼容。如果存在,则限制通常为15或16个字符。

ar被认为是二进制实用程序,因为此类归档最常用作包含常用子例程的库。

当您指定修饰符s时,ar将为在存档中可重定位目标模块中定义的符号创建索引。创建索引后,只要ar对其内容进行更改(在q更新操作中保存),该索引就会在存档中更新。具有这种索引的存档可以加快链接到库的速度,并允许库中的例程相互调用,而无需考虑它们在存档中的位置。

您可以使用nm -snm --print-armap列出此索引表。如果归档文件中没有该表,则可以使用另一种形式的ranlib来仅添加表。

GNU ar可以选择创建一个精简归档文件,该归档文件包含符号索引和对归档文件成员文件原始副本的引用。这样的档案对于构建供本地构建使用的库很有用,在本地构建中可重定位的对象应该保持可用,并且复制每个对象的内容只会浪费时间和空间。精简归档文件也被展平,因此将一个或多个归档文件添加到精简归档文件将分别添加嵌套归档文件的元素。档案元素的路径是相对于档案本身存储的。

GNU ar被设计为与两种不同的设施兼容。您可以使用命令行选项来控制其活动,例如Unix系统上ar的不同种类;或者,如果您指定单个命令行选项-M,则可以使用通过标准输入提供的脚本来控制它,例如MRI“图书馆员”程序。

The ar program creates, modifies, and extracts from archives. An archive is a single file holding a collection of other files in a structure that makes it possible to retrieve the original individual files (called members of the archive).

The original files' contents, mode (permissions), timestamp, owner, and group are preserved in the archive, and can be restored on extraction.

GNU ar can maintain archives whose members have names of any length; however, depending on how ar is configured on your system, a limit on file name length may be imposed for compatibility with archive formats used by other tools. If it exists, the limit is often 15 or 16 characters.

ar is considered a binary utility because archives of this sort are most often used as libraries holding commonly needed subroutines.

ar creates an index to the symbols defined in relocatable object modules in the archive when you specify the modifier s. Once created, this index is updated in the archive whenever ar makes a change to its contents (save for the q update operation). An archive with such an index speeds up linking to the library, and allows routines in the library to call each other without regard to their placement in the archive.

You may use nm -s or nm --print-armap to list this index table. If an archive lacks the table, another form of ar called ranlib can be used to add just the table.

GNU ar can optionally create a thin archive, which contains a symbol index and references to the original copies of the member files of the archives. Such an archive is useful for building libraries for use within a local build, where the relocatable objects are expected to remain available, and copying the contents of each object would only waste time and space. Thin archives are also flattened, so that adding one or more archives to a thin archive will add the elements of the nested archive individually. The paths to the elements of the archive are stored relative to the archive itself.

GNU ar is designed to be compatible with two different facilities. You can control its activity using command-line options, like the different varieties of ar on Unix systems; or, if you specify the single command-line option -M, you can control it with a script supplied via standard input, like the MRI "librarian" program.

查看英文版

查看中文版

ar 语法

ar [--plugin name] [-X32_64] [-]p[mod [relpos] [count]]
   [--target bfdname] archive [member...]

选件

GNU ar允许您在第一个命令行参数内以任何顺序混合操作代码p和修饰符标志。

如果需要,可以将第一个命令行参数以破折号开头。

p关键字指定要执行的操作; 可能是以下任何一种,但您只能指定其中之一:

操作方式:

d

从存档中删除模块。将要删除的模块名称指定为“ member ... ”;如果您没有指定要删除的文件,则归档不会被更改。

如果指定v修饰符,则ar列出每个模块,因为它们已被删除。

m

使用此操作可移动档案中的成员。

如果在一个以上的成员中定义了符号,则归档中成员的顺序可能会影响使用库链接程序的方式。

如果“ m ” 未使用修饰符,则您在成员参数中命名的所有成员都将移至归档文件的末尾;您可以使用a,bi修饰符将其移动到指定位置。

p

将存档的指定成员打印到标准输出文件。如果指定了v修饰符,请在将其内容复制到标准输出之前显示成员名称。

如果未指定成员参数,则将打印存档中的所有文件。

q

快速追加;从历史上看,将文件“ member ... ” 添加到存档末尾,而不检查是否替换。

修饰符a,bi不会影响此操作;新成员总是放在档案的末尾。

修饰符v使ar在附加文件时列出每个文件。

由于此操作的重点是速度,因此即使归档文件的符号表索引已经存在,也不会更新。您可以显式使用arranlib更新符号表索引。

但是,有太多不同的系统假定快速追加会重建索引,因此GNU arq实现为r的同义词。

r

将文件“ member ... ”插入存档(替换)。此操作与q的不同之处在于,如果任何先前存在的成员的名称与要添加的成员相匹配,则将其删除。

如果以“ member ... ” 命名的文件之一不存在,ar会显示一条错误消息,并使与该名称匹配的归档的所有现有成员不受干扰。

默认情况下,新成员添加在文件末尾;但是您可以使用修饰符a,bi之一请求相对于某些现有成员的位置。

与该操作一起使用的修饰符v会为插入的每个文件引发一行输出,以及字母ar之一,以指示是追加文件(未删除旧成员)还是替换文件。

s

将索引添加到存档中,或者如果已存在则对其进行更新。请注意,此命令是一个例外,因为该规则只能有一个命令字母,因为可以将其用作命令或修饰符。无论哪种情况,它都做同样的事情。

t

显示一个表,该表列出了归档的内容,或归档中存在的member ...中列出的文件的内容。通常只显示成员名称;如果您还想查看模式(权限),时间戳记,所有者,组和大小,则可以通过指定v修饰符来请求。

如果未指定成员,则会列出存档中的所有文件。

x

从存档中提取成员(名为member)。您可以在此操作中使用v修饰符,以要求ar在提取每个名称时列出每个名称。

如果您未指定member,那么将提取存档中的所有文件。

无法从精简存档中提取文件。

--help

显示ar支持的命令行选项列表,然后退出。

--version

显示ar的版本信息,然后退出。

p修饰符后面可以紧跟许多修饰符,以指定操作行为的变化:

修饰符:

a

在存档的现有成员之后添加新文件。如果使用修饰符a,则在归档规范之前,现有归档成员的名称必须作为relpos参数出现。

b

在存档的现有成员之前添加新文件。如果使用修饰符b,则在归档规范之前,现有归档成员的名称必须作为relpos参数出现。(和i一样)。

c

创建档案。当请求更新时,将始终创建指定的归档(如果不存在)。但是会发出警告,除非您事先使用此修饰符指定要创建它。

D

以确定性模式操作。添加文件和存档索引时,对于UID,GID,时间戳记,请使用零;对于所有文件,请使用一致的文件模式。使用此选项时,如果ar与相同的选项和相同的输入文件一起使用,则无论输入文件的所有者,组,文件模式或修改时间如何,多次运行都会创建相同的输出文件。

如果binutils配置了--enable-deterministic-archives,则默认情况下此模式为开。可以使用下面的U修改器禁用它。

f

截断档案中的名称。GNU ar通常会允许使用任何长度的文件名。这将导致它创建与某些系统上的本机ar程序不兼容的存档。如果这是一个问题,则在将文件名放入归档文件时,可以使用f修饰符截断文件名。

i

在档案的现有成员之前插入新文件。如果使用修饰符i,则在归档规范之前,必须将现有归档成员的名称作为relpos参数出现。(与b相同)。

l

此修饰符是可接受的,但不使用。

N

使用count参数。如果归档中有多个具有相同名称的条目,则使用此选项。从归档中提取或删除给定名称的实例计数。

o

提取成员时保留其原始日期。如果未指定此修饰符,则从归档文件中提取的文件会标有提取时间。

P

在归档中匹配名称时,请使用完整路径名。GNU ar无法创建具有完整路径名的档案(此类档案不是POSIX投诉),但是其他档案创建者可以。此选项将使GNU ar使用完整的路径名来匹配文件名,这在从另一个工具创建的档案中提取单个文件时会很方便。

s

将目标文件索引写入存档,或更新现有索引,即使对存档没有其他更改。您可以通过任何操作或单独使用此修饰符标志。在存档上运行ar等效于在存档上运行ranlib

S

不生成档案符号表。这样可以分几步加快构建大型库的速度。生成的存档不能与链接器一起使用。要构建符号表,必须在最后一次执行ar时省略S修饰符,或者必须在归档文件上运行ranlib

T

将指定的存档设为精简存档。如果已经存在并且是常规存档,则现有成员必须与存档位于同一目录中。

u

通常,ar r ...将列出的所有文件插入存档。如果您只想插入列出的文件中比同名现有成员新的文件,请使用此修饰符。该ü修饰符只允许操作[R(“替换”)。特别地,不允许组合qu,因为检查时间戳将失去操作q的任何速度优势。

U

不要在确定性模式下运行。这与上面的D修饰符相反:添加的文件和存档索引将获得其实际的UID,GID,时间戳和文件模式值。

这是默认设置,除非binutils配置了--enable-deterministic-archives

v

此修饰符要求操作的详细版本。附加修饰符v时,许多操作都会显示其他信息,例如已处理的文件名。

V

此修饰符显示ar的版本号。

Other:

@file

从file中读取命令行选项。读取的选项将插入到原始@ 文件选项中。如果文件不存在或无法读取,则该选项将按字面意义处理,并且不会删除。

文件中的选项用空格分隔。通过将整个选项括在单引号或双引号中,可以在选项中包括空格字符。通过在要包含的字符前面加上反斜杠,可以包含任何字符(包括反斜杠)。该文件本身可能包含其他@ 文件选项;任何此类选项将被递归处理。

ar [--plugin name] [-X32_64] [-]p[mod [relpos] [count]]
   [--target bfdname] archive [member...]

Options

GNU ar allows you to mix the operation code p and modifier flags in any order, within the first command-line argument.

If you want, you can begin the first command-line argument with a dash.

The p keyletter specifies what operation to execute; it may be any of the following, but you must specify only one of them:

Operations:

d

Delete modules from the archive. Specify the names of modules to be deleted as "member..."; the archive is untouched if you specify no files to delete.

If you specify the v modifier, ar lists each module as it is deleted.

m

Use this operation to move members in an archive.

The ordering of members in an archive can make a difference in how programs are linked using the library, if a symbol is defined in more than one member.

If no modifiers are used with "m", any members you name in the member arguments are moved to the end of the archive; you can use the ab, or i modifiers to move them to a specified place instead.

p

Print the specified members of the archive, to the standard output file. If the v modifier is specified, show the member name before copying its contents to standard output.

If you specify no member arguments, all the files in the archive are printed.

q

Quick append; Historically, add the files "member..." to the end of archive, without checking for replacement.

The modifiers ab, and i do not affect this operation; new members are always placed at the end of the archive.

The modifier v makes ar list each file as it is appended.

Since the point of this operation is speed, the archive's symbol table index is not updated, even if it already existed; you can use ar s or ranlib explicitly to update the symbol table index.

However, too many different systems assume quick append rebuilds the index, so GNU ar implements q as a synonym for r.

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.

If one of the files named in "member..." does not exist, ar displays an error message, and leaves undisturbed any existing members of the archive matching that name.

By default, new members are added at the end of the file; but you may use one of the modifiers ab, or i to request placement relative to some existing member.

The modifier v used with this operation elicits a line of output for each file inserted, along with one of the letters a or r to indicate whether the file was appended (no old member deleted) or replaced.

s

Add an index to the archive, or update it if it already exists. Note this command is an exception to the rule that there can only be one command letter, as it is possible to use it as either a command or a modifier. In either case it does the same thing.

t

Display a table listing the contents of archive, or those of the files listed in member... that are present in the archive. Normally only the member name is shown; if you also want to see the modes (permissions), timestamp, owner, group, and size, you can request that by also specifying the v modifier.

If you do not specify a member, all files in the archive are listed.

x

Extract members (named member) from the archive. You can use the v modifier with this operation, to request that ar list each name as it extracts it.

If you do not specify a member, all files in the archive are extracted.

Files cannot be extracted from a thin archive.

--help

Displays the list of command line options supported by ar and then exits.

--version

Displays the version information of ar and then exits.

A number of modifiers may immediately follow the p keyletter, to specify variations on an operation's behavior:

Modifiers:

a

Add new files after an existing member of the archive. If you use the modifier a, the name of an existing archive member must be present as the relpos argument, before the archive specification.

b

Add new files before an existing member of the archive. If you use the modifier b, the name of an existing archive member must be present as the relpos argument, before the archive specification. (same as i).

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.

D

Operate in deterministic mode. When adding files and the archive index use zero for UIDs, GIDs, timestamps, and use consistent file modes for all files. When this option is used, if ar is used with identical options and identical input files, multiple runs will create identical output files regardless of the input files' owners, groups, file modes, or modification times.

If binutils was configured with --enable-deterministic-archives, then this mode is on by default. It can be disabled with the U modifier, below.

f

Truncate names in the archive. GNU ar will normally permit file names of any length. This will cause it to create archives that are not compatible with the native ar program on some systems. If this is a concern, the f modifier may be used to truncate file names when putting them in the archive.

i

Insert new files before an existing member of the archive. If you use the modifier i, the name of an existing archive member must be present as the relpos argument, before the archive specification. (same as b).

l

This modifier is accepted but not used.

N

Uses the count parameter. This is used if there are multiple entries in the archive with the same name. Extract or delete instance count of the given name from the archive.

o

Preserve the original dates of members when extracting them. If you do not specify this modifier, files extracted from the archive are stamped with the time of extraction.

P

Use the full path name when matching names in the archive. GNU ar can not create an archive with a full path name (such archives are not POSIX complaint), but other archive creators can. This option will cause GNU ar to match file names using a complete path name, which can be convenient when extracting a single file from an archive created by another tool.

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.

S

Do not generate an archive symbol table. This can speed up building a large library in several steps. The resulting archive can not be used with the linker. To build a symbol table, you must omit the S modifier on the last execution of ar, or you must run ranlib on the archive.

T

Make the specified archive a thin archive. If it already exists and is a regular archive, the existing members must be present in the same directory as archive.

u

Normally, ar r... inserts all files listed into the archive. If you would like to insert only those of the files you list that are newer than existing members of the same names, use this modifier. The u modifier is allowed only for the operation r ("replace"). In particular, the combination qu is not allowed, since checking the timestamps would lose any speed advantage from the operation q.

U

Do not operate in deterministic mode. This is the inverse of the D modifier, above: added files and the archive index will get their actual UID, GID, timestamp, and file mode values.

This is the default unless binutils was configured with --enable-deterministic-archives.

v

This modifier requests the verbose version of an operation. Many operations display additional information, such as file names processed, when the modifier v is appended.

V

This modifier shows the version number of ar.

Other:

@file

Read command-line options from file. The options read are inserted in place of the original @file option. If file does not exist, or cannot be read, then the option will be treated literally, and not removed.

Options in file are separated by whitespace. A whitespace character may be included in an option by surrounding the entire option in either single or double quotes. Any character (including a backslash) may be included by prefixing the character to be included with a backslash. The file may itself contain additional @file options; any such options will be processed recursively.

查看英文版

查看中文版

ar 例子

ar r myfile.a 'ls *.o'

myfile.a替换为当前目录中的目标文件。

ar r myfile.a 'ls *.o'

Replace myfile.a with object files in the current directory.

查看英文版

查看中文版

其他命令行

apt-cache | apt-get | arch | arp | as | a2p | aspell | ac | at | awk | adduser | a2p | apropos | alias | agrep | addgroup |

如此好文,分享给朋友
发表评论
验证码:
评论列表
共0条