分类
devops

ar and tar

current ar archive 这个语境下,ar 的含义非常直观:

ar 代表 “Archive”(归档/存档)

在 Linux/Unix 编程工具链中,ar 是一个标准的命令行工具,它的核心作用就是将多个文件(主要是 .o 目标文件)打包成一个单独的存档文件(即 .a 静态库文件)


为什么需要 ar

可以把 .o 文件看作是乐高积木的零件,而 .a 静态库就是“积木礼盒”。
* 如果有 100 个 .o 文件,分发给别人时,对方很难管理,也很容易丢。
* 使用 ar 命令,可以把这 100 个零件压缩进一个 .a 文件里,这样分发起来就非常方便。

artar 的区别(常见误区)

很多人会疑惑,Linux 下已经有了 tar(Tape Archive),为什么还要有一个 ar

特性 ar (Archive) tar (Tape Archive)
主要用途 专门为静态库设计 通用归档(备份、源码发布)
文件结构 极其简单,专门为了被链接器(Linker)快速读取而优化 包含复杂的文件系统元数据(权限、目录结构、符号链接)
对文件的处理 不包含子目录信息(通常是扁平的) 完整保留目录树和文件权限
典型场景 生成 .a 文件 压缩备份、分发安装包

ar 是一个“轻量级”的、专为编译链接场景定制的归档工具。它并不关心目录结构或权限,它只关心如何高效地把一堆二进制代码块(.o)粘在一起,以便于链接器(Linker)在编译时能快速扫描和提取其中的函数。

tar 在早期,计算机数据存储主要使用磁带作为介质,因此 Tape Archive 这个名字反映了当时的技术现状。

在 1979 年,tar 命令由 GNU 项目的创始人 Richard Stallman 创建,当时主要用于将多个文件打包成一个文件,以便于在磁带上存储和传输。

尽管现在的数据存储技术已经发展到可以使用硬盘、固态硬盘等更为先进的介质,但是 tar 命令仍然沿用了当时的名称。

所以,tar 文件中的 tar 是 Tape Archive 的缩写,而不是磁带的意思。它只是一个历史遗留名称,代表了一种打包和归档文件的方式。

jar 文件中的 jar 是 Java ARchive 的缩写。

在 Java 语言中,jar 文件是一种用于打包 Java 类文件、资源文件和其他相关文件的文件格式。

jar 文件是在 1995 年由 Sun Microsystems(现为 Oracle 公司)引入的,用于将 Java 类文件和其他资源文件打包成一个文件,以便于在 Java 虚拟机(JVM)上运行。

jar 文件实际上是一种压缩文件,使用 ZIP 格式进行压缩和存储。因此,jar 文件可以被视为一种特殊的 ZIP 文件。

在 jar 文件中,ar 是 archive 的缩写,而不是指代其他含义。

rar 文件格式中的第一个 R 表示 Roshal,这是该文件格式的创造者 Eugene Roshal 的姓氏。

rar 文件格式是一种压缩文件格式,由 Eugene Roshal 在 1993 年创建。rar 文件格式在 Windows 平台上非常流行,广泛用于压缩和归档文件。

rar 文件格式中的 R 表示 Roshal 的姓氏,而 ar 则表示 Archive(归档)。

所以,rar 文件格式的全称是 Roshal ARchive。

实际操作示例

当运行 ar rcs libmylib.a file1.o file2.o 时:
* r (Replace/Insert):如果有同名文件,替换它;如果没有,添加它。
* c (Create):如果目标库不存在,自动创建它。
* s (Symbol index)这最重要。它会在库里创建一个“符号索引表”(类似于书的目录),让链接器在寻找函数时,不需要逐个扫描文件,直接通过索引就能定位函数位置,极大提升了链接速度。

所以,ar 就是那个负责把散落的零件(.o)“打包并编目录”的工具,最终产物就是静态库(.a)。

ar 命令通常包含在 binutils (Binary Utilities) 这个软件包中。

在 Linux 世界中,binutils 是开发和系统管理中最基础的工具包之一。它几乎在所有 Linux 发行版中都是预装的,因为它是编译环境(如 GCC)的必要依赖。

发行版系列 安装命令
Debian / Ubuntu / Kali sudo apt update && sudo apt install binutils
RHEL / CentOS / Fedora / openEuler sudo dnf install binutils
Arch Linux sudo pacman -S binutils
ar --help
Usage: ar [emulation options] [-]{dmpqrstx}[abcDfilMNoOPsSTuvV] [--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[O][v]      - display contents of the 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
  [O]          - display offsets of files in the archive
  [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)
  [l <text> ]  - specify the dependencies of this library
  [S]          - do not build a symbol table
  [T]          - deprecated, use --thin instead
  [v]          - be verbose
  [V]          - display the version number
  @<file>      - read options from <file>
  --target=BFDNAME - specify the target object format as BFDNAME
  --output=DIRNAME - specify the output directory for extraction operations
  --record-libdeps=<text> - specify the dependencies of this library
  --thin       - make a thin archive
 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 pei-i386 pe-x86-64 pei-x86-64 elf64-little elf64-big elf32-little elf32-big pe-bigobj-x86-64 pe-i386 pdb srec symbolsrec verilog tekhex binary ihex plugin
Report bugs to <https://gitlab.alpinelinux.org/alpine/aports/-/issues>
ar --help
Usage: ar [emulation options] [-]{dmpqrstx}[abcDfilMNoOPsSTuvV] [--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[O][v]      - display contents of the 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
  [O]          - display offsets of files in the archive
  [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)
  [l <text> ]  - specify the dependencies of this library
  [S]          - do not build a symbol table
  [T]          - deprecated, use --thin instead
  [v]          - be verbose
  [V]          - display the version number
  @<file>      - read options from <file>
  --target=BFDNAME - specify the target object format as BFDNAME
  --output=DIRNAME - specify the output directory for extraction operations
  --record-libdeps=<text> - specify the dependencies of this library
  --thin       - make a thin archive
 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 pei-i386 pe-x86-64 pei-x86-64 elf64-little elf64-big elf32-little elf32-big pe-bigobj-x86-64 pe-i386 pdb srec symbolsrec verilog tekhex binary ihex plugin
Report bugs to <https://sourceware.org/bugzilla/>