UNIX ar commands

Source: Internet
Author: User

UNIX ar commands
Common options

After the source code file is compiled, the corresponding target file (. o file) is generated. Generally, the process of creating a static library is to package these. o files into a libXXXX. a file.
The meanings of several common commands for ar commands are as follows:

-R format: ar-r libXXXX. a 1.o 2. o Note: When the lib file does not exist, first create the lib file and then. o file added to static library file-t format: ar-t libXXXX. a description: displays the information contained in the current lib file. o file-d format: ar-d libXXXX. a 1. o Description: Set 1. o from the static library file libXXXX. delete-x in the format of ar-x libXXXX. a description: Use the static library file libXXXX. decompress all the files in a and edit them now. c files, respectively. 1. c # include <stdio. h>
Void func1 (){
Printf ("call func1. \ n ");
}

// 2.c

# Include <stdio. h>
Void func2 (){
Printf ("call func2. \ n ");
}

Perform the following operations:

[Linux@www.bkjia.com ar] $ ls
1. c 2.c
[Linux@www.bkjia.com ar] $ gcc-c 1.c 2.c
[Linux@www.bkjia.com ar] $ ls
1. c 1.o 2.c 2.o
[Linux@www.bkjia.com ar] $ ar-r libtest. a 1.o
Ar: creating libtest.
[Linux@www.bkjia.com ar] $ ar-r libtest. a 2.o
[Linux@www.bkjia.com ar] $ ar-t libtest.
1. o
2. o
[Linux@www.bkjia.com ar] $ ar-d libtest. a 1.o
[Linux@www.bkjia.com ar] $ ar-t libtest.
2. o
[Linux@www.bkjia.com ar] $ ar-r libtest. a 1.o
[Linux@www.bkjia.com ar] $ rm-rf 1.o 2.o
[Linux@www.bkjia.com ar] $ ls
1. c 2.c libtest.
[Linux@www.bkjia.com ar] $ ar-x libtest.
[Linux@www.bkjia.com ar] $ ls
1. c 1.o 2.c 2.o libtest.

In particular, it indicates the-s usage of the ar command. the old version of the UNIX operating system is added with ar. o file, if. the o file already exists in the static library and will be replaced with this.. o The file suffix is at the end of the static library file. then, when you use ld for link, the system may report an error where the function or global variable cannot be found. This is because the symbol index table information in the static library file is not updated, and the symbol table information in the old. o file is still used. To overcome this problem, the ranlib tool is provided to update the symbol table. Therefore, the following command is usually seen in makefiles of older UNIX programs: libXXXX. a: 1.o 2.o... ar-rv libXXXX. a 1.o 2.o... ranlib libXXXX. by default, the ar command of Version a updates the symbol table, so ranlib is no longer required. Because the symbol table is updated by default, the functions of ar-respiratory and ar-rv are the same. You can use the nm command to view the symbol table information in libXXXX.. For example:

[Linux@www.bkjia.com ar] $ nm-s libtest.

Archive index:
Func1 in 1.o
Func2 in 2.o

1. o:
0000000000000000 T func1
U puts

2. o:
0000000000000000 T func2
U puts
[Linux@www.bkjia.com ar] $

This article permanently updates the link address:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.