ar 이용하기
ar 명령은 정적 라이브러리를 생성, 조회, 편집하기 위한 명령어이다.

먼저 help를 보겠습니다.
root@boggle70-desktop:tmp# ar
Usage: ar [emulation options] [-]{dmpqrstx}[abcfilNoPsSuvV] [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
 [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>
emulation options: 
 No emulation specific options
ar: supported targets: elf32-i386 a.out-i386-linux pei-i386 elf32-little elf32-big srec symbolsrec verilog tekhex binary ihex trad-core


내용이 쉬운 관계로 /usr/lib/libcrypt.a 라는 정적 라이브러리를 하나 가져와서 한번 살펴봅니다.
root@boggle70-desktop:tmp# ll
-rw-r--r-- 1 root root  44978 2011-04-02 19:37 libcrypt.a
아카이브의 정보를 조회하기 위해서 아래와 같이 합니다.
root@boggle70-desktop:tmp# ar tv libcrypt.a 
rw-r--r-- 0/0   2080 May 21 17:17 2010 crypt-entry.o
rw-r--r-- 0/0   4408 May 21 17:17 2010 md5-crypt.o
rw-r--r-- 0/0   6432 May 21 17:17 2010 sha256-crypt.o
rw-r--r-- 0/0   8488 May 21 17:17 2010 sha512-crypt.o
rw-r--r-- 0/0    993 May 21 17:17 2010 crypt.o
rw-r--r-- 0/0   9620 May 21 17:17 2010 crypt_util.o
rw-r--r-- 0/0   4360 May 21 17:17 2010 md5.o
rw-r--r-- 0/0   2732 May 21 17:17 2010 sha256.o
rw-r--r-- 0/0   4564 May 21 17:17 2010 sha512.o

이렇게 9개의 오브젝트로 이루어져 있습니다.
그런데 이중에서 crypt-entry.o 오픈 소스가 아닌 독점소스로서 제거를 해야 하는데
나머지 라이브러리들의 소스가 없어 빌드를 할수도 없습니다.

이제 crypt-entry.o 를 제거해 봅니다.
root@boggle70-desktop:tmp# ar d libcrypt.a crypt-entry.o
너무 간단하네요. 한번 잘 되었는지 확인해 봅니다.
root@boggle70-desktop:tmp# ar tv libcrypt.a 
rw-r--r-- 0/0   4408 May 21 17:17 2010 md5-crypt.o
rw-r--r-- 0/0   6432 May 21 17:17 2010 sha256-crypt.o
rw-r--r-- 0/0   8488 May 21 17:17 2010 sha512-crypt.o
rw-r--r-- 0/0    993 May 21 17:17 2010 crypt.o
rw-r--r-- 0/0   9620 May 21 17:17 2010 crypt_util.o
rw-r--r-- 0/0   4360 May 21 17:17 2010 md5.o
rw-r--r-- 0/0   2732 May 21 17:17 2010 sha256.o
rw-r--r-- 0/0   4564 May 21 17:17 2010 sha512.o
이제 오픈소스로 구현된 새로운 crypt-entry.o 를 대치할 우리의 친구 foo.o를 넣어 봅니다.
root@boggle70-desktop:tmp# ar rcus libcrypt.a foo.o
잘 되었는지 한번 확인해 봅니다.
root@boggle70-desktop:tmp# ar tv libcrypt.a 
rw-r--r-- 0/0   4408 May 21 17:17 2010 md5-crypt.o
rw-r--r-- 0/0   6432 May 21 17:17 2010 sha256-crypt.o
rw-r--r-- 0/0   8488 May 21 17:17 2010 sha512-crypt.o
rw-r--r-- 0/0    993 May 21 17:17 2010 crypt.o
rw-r--r-- 0/0   9620 May 21 17:17 2010 crypt_util.o
rw-r--r-- 0/0   4360 May 21 17:17 2010 md5.o
rw-r--r-- 0/0   2732 May 21 17:17 2010 sha256.o
rw-r--r-- 0/0   4564 May 21 17:17 2010 sha512.o
rw-r--r-- 0/0  14532 Feb 26 20:28 2011 foo.o

이제 이 아카이브를 모두 분해해 봅니다.
root@boggle70-desktop:tmp# ar xv libcrypt.a 
x - md5-crypt.o
x - sha256-crypt.o
x - sha512-crypt.o
x - crypt.o
x - crypt_util.o
x - md5.o
x - sha256.o
x - sha512.o
x - foo.o

아래와 같이 잘 분리되어 나왔습니다.
root@boggle70-desktop:tmp# ll
-rw-r--r-- 1 root root    993 2011-04-02 19:45 crypt.o
-rw-r--r-- 1 root root   9620 2011-04-02 19:45 crypt_util.o
-rw-r--r-- 1 root root  14532 2011-04-02 19:45 foo.o
-rw-r--r-- 1 root root  57458 2011-04-02 19:43 libcrypt.a
-rw-r--r-- 1 root root   4408 2011-04-02 19:45 md5-crypt.o
-rw-r--r-- 1 root root   4360 2011-04-02 19:45 md5.o
-rw-r--r-- 1 root root   6432 2011-04-02 19:45 sha256-crypt.o
-rw-r--r-- 1 root root   2732 2011-04-02 19:45 sha256.o
-rw-r--r-- 1 root root   8488 2011-04-02 19:45 sha512-crypt.o
-rw-r--r-- 1 root root   4564 2011-04-02 19:45 sha512.o

이제 새로운 이름으로 오브젝트를 묶어 봅니다.
root@boggle70-desktop:tmp# ar rcus libboggle70.a *.o
r 옵션은 맨뒤 help 에서 보았듯이 새로운 파일이면 추가, 기존 파일이면 치환시킵니다.
c 옵션은 libboggle70.a 가 없어서 새로운 파일을 만들어도 경고를 내지 않도록 합니다.
u 옵션은 오브젝트 파일의 타임스탬프를 비교해 새로운 파일일 경우에만 치환하도록 합니다.
s 옵션은 아카이브 인덱스를 생성하도록 해서 
                      링크속도가 느려지거나 에러 발생이 되지 않도록 해줍니다.
    이러한 아카이브 인덱스는 nm -s libboggle70.a 와 같이 해서 확인이 가능합니다.

역시 그 내용을 확인해 보면 방금 작업한 내용들이 모두 들어있습니다.
root@boggle70-desktop:tmp# ar tv libboggle70.a 
rw-r--r-- 0/0   1108 Feb 26 15:45 2011 bar.o
rw-r--r-- 0/0    993 Apr  2 19:45 2011 crypt.o
rw-r--r-- 0/0   9620 Apr  2 19:45 2011 crypt_util.o
rw-r--r-- 0/0  14532 Apr  2 19:45 2011 foo.o
rw-r--r-- 0/0   4408 Apr  2 19:45 2011 md5-crypt.o
rw-r--r-- 0/0   4360 Apr  2 19:45 2011 md5.o
rw-r--r-- 0/0   6432 Apr  2 19:45 2011 sha256-crypt.o
rw-r--r-- 0/0   2732 Apr  2 19:45 2011 sha256.o
rw-r--r-- 0/0   8488 Apr  2 19:45 2011 sha512-crypt.o
rw-r--r-- 0/0   4564 Apr  2 19:45 2011 sha512.o

오픈 소스로 제공되지 않는 아카이브의 경우 ar 을 이용해서 오브젝트를 분리해내고
재구성과 분리를 해내는 것이 가능합니다.





출처  : Binary Hacks - O'REILLY