Linux 系统备份压缩命令 - ar

Linux 系统备份压缩命令 - ar

ar 是 Linux 系统的一个备份压缩命令,用于创建、修改备存文件 (archive) 或从备存文件中提取成员文件。备存文件以一定的结构打包一个至多个其它文件 (成员文件),且成员文件的内容、模式、时间戳等信息将被保存在备存文件中。使用 ar 命令将多个目标文件 *.o 打包为静态链接库文件 *.a

静态库是目标文件的一个集合,通常静态库以 .a 为后缀。静态库由程序 ar 生成。静态库不再普遍出现,主要是由于现在的程序一般都使用动态库。

1. 生成静态链接库

工具 ar 的选项 -r,可以创建库,并把目标文件插入到指定库中。目标文件一般为 C 语言源文件 *.c 编译后的文件 *.o

$ar -r LibName.a ObjFile.o [ObjFile2.o] [ObjFile3.o] [...]

例如将 string.o 打包为库文件 libstr.a 的命令为:

$ar -rcs libstr.a string.o

2. 使用静态链接库

在编译程序的时候经常需要使用函数库,例如经常使用的 C 标准库等。GCC 链接时使用库函数和一般的目标文件的形式是一致的。
例如 main.c 中使用了 libstr.a 中的函数,生成最终的可执行文件 main.out 的命令如下:

$gcc -o main.out main.c libstr.a

3. ar - create, modify, and extract from archives

3.1 NAME
ar - create, modify, and extract from archives

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

3.3 DESCRIPTION
The GNU 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).

3.4 --version
Displays the version information of ar and then exits.

strong@foreverstrong:~$ ar --version
GNU ar (GNU Binutils for Ubuntu) 2.26.1
Copyright (C) 2015 Free Software Foundation, Inc.
This program is free software; you may redistribute it under the terms of
the GNU General Public License version 3 or (at your option) any later version.
This program has absolutely no warranty.
strong@foreverstrong:~$ 

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

strong@foreverstrong:~$ ar --help
Usage: ar [emulation options] [-]{dmpqrstx}[abcDfilMNoPsSTuvV] [--plugin <name>] [member-name] [count] archive-file file...
       ar -M [<mri-script]
 commands:
  d            - delete file(s) from the archive
  m[ab]        - move file(s) in the archive
  p            - print file(s) found in the archive
  q[f]         - quick append file(s) to the archive
  r[ab][f][u]  - replace existing or insert new file(s) into the archive
  s            - act as ranlib
  t            - display contents of archive
  x[o]         - extract file(s) from the archive
 command specific modifiers:
  [a]          - put file(s) after [member-name]
  [b]          - put file(s) before [member-name] (same as [i])
  [D]          - use zero for timestamps and uids/gids (default)
  [U]          - use actual timestamps and uids/gids
  [N]          - use instance [count] of name
  [f]          - truncate inserted file names
  [P]          - use full path names when matching
  [o]          - preserve original dates
  [u]          - only replace files that are newer than current archive contents
 generic modifiers:
  [c]          - do not warn if the library had to be created
  [s]          - create an archive index (cf. ranlib)
  [S]          - do not build a symbol table
  [T]          - make a thin archive
  [v]          - be verbose
  [V]          - display the version number
  @<file>      - read options from <file>
  --target=BFDNAME - specify the target object format as BFDNAME
 optional:
  --plugin <p> - load the specified plugin
 emulation options: 
  No emulation specific options
ar: supported targets: elf64-x86-64 elf32-i386 elf32-iamcu elf32-x86-64 a.out-i386-linux pei-i386 pei-x86-64 elf64-l1om elf64-k1om elf64-little elf64-big elf32-little elf32-big pe-x86-64 pe-bigobj-x86-64 pe-i386 plugin srec symbolsrec verilog tekhex binary ihex
Report bugs to <http://www.sourceware.org/bugzilla/>
strong@foreverstrong:~$

3.6 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.

modifier ['mɒdɪfaɪə]:n. 改性剂,修饰语,修正的人

3.7 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.

猜你喜欢

转载自blog.csdn.net/chengyq116/article/details/87888898