The AR command can be used to create, modify, or present a single module from a library. A library is a separate file that contains other files that are organized according to a particular structure (referred to as member of this library file). The original file's content, pattern, timestamp, owner, group, and so on are kept in the library file.
AR allows you to assemble many files into a single, stored file. In a saved file, all member files retain their original attributes and permissions.
Purpose description
Create a static library. A file. It's often used when developing programs with C + +, but I rarely use the AR command on the command line alone, which is generally written in makefile and sometimes used in shell scripts. For Linux library files, static libraries, dynamic libraries and how to create and use related knowledge, see the relevant information later in this article "3" on Linux static Library and dynamic library analysis.
Common parameters
Format: ar RCS libxxx.a xx1.o xx2.o
Parameter r: Inserts a module (replace) in the library. Replaces a module with the same name when the inserted module name already exists in the library. If a module in several modules does not exist in the library, AR displays an error message and does not replace other modules with the same name. By default, new members are added at the end of the library, and other options can be used to change the added position. "1"
Parameter c: Create a library. is created regardless of whether the library exists.
Parameter s: Creates a target file index, which speeds up time when creating a larger library. (Supplemental: If you do not need to create an index, you can change it to uppercase S parameters; A file is missing an index and can be added using the ranlib command
Format: AR t libxxx.a
Displays which destination files are in the library file and displays only the names.
Format: AR TV libxxx.a
Shows which target files are in the library file, displaying the file name, time, size, and more details.
Format: Nm-s libxxx.a
Displays the index table in the library file.
Format: Ranlib libxxx.a
Create an index table for the library file.
Using the sample
Example one is used in a shell script
Bash code
os= ' uname-r '
ar rcs libhycu.a. $OS *.O
Example two uses in makefile
Makefile Code
$ (BIN1): $ (BIN1_OBJS)
ar RCS $@ $^
Example three create and use a static library
The first step: Edit the source file, Test.h test.c main.c. Where the Main.c file contains the main function as a program entry, and TEST.C contains the functions that are needed in the main function.
VI test.h test.c MAIN.C
Step two: Compile the test.c into the target file.
Gcc-c test.c
If test.c is correct, you will get TEST.O this target file.
Step three: By. o file to create a static library.
AR RCS LIBTEST.A TEST.O
Step Fourth: Use the static library in your program.
Gcc-o main main.c-l.-ltest
Because it is statically compiled, the resulting execution file can be independent of the. A file runs.
Fifth step: implementation.
./main
Example four creating and using dynamic libraries
The first step: Edit the source file, Test.h test.c main.c. Where the Main.c file contains the main function as a program entry, and TEST.C contains the functions that are needed in the main function.
VI test.h test.c MAIN.C
Step two: Compile the test.c into the target file.
Gcc-c test.c
The previous two steps are consistent with creating a static library.
Step three: By. o file to create a dynamic library file.
Gcc-shared-fpic-o libtest.so TEST.O
Step Fourth: Use dynamic libraries in your programs.
Gcc-o main main.c-l.-ltest
When a static library and a dynamic library have the same name, the GCC command will use the dynamic library as a priority.
Fifth step: implementation.
Ld_library_path=. ./main
Example five view files in a static library
[root@node56 lib]# ar-t libhycu.a base64.c.o binbuf.c.o cache.c.o chunk.c.o codec_a.c.o
> The ... XORT.C.O
[root@node56 lib]#
[root@node56 lib]# ar-tv libhycu.a rw-r--r--0/0 7220 June 29 19:18 20 One base64.c.o
rw-r--r--0/0 2752 June 19:18) BINBUF.C.O rw-r--r--0/0 19768 June
19:18 CAC HE.C.O ...
rw-r--r--0/0 4580 19:18 xort.c.o
[root@node56 lib]# [root@node56 lib]# nm-s libhycu.a
| l ESS
Archive Index:
base64enc in BASE64.C.O,
getbase64value in BASE64.C.O
, Base64dec in base64.c. o
encode64 in base64.c.o
decode64 in BASE64.C.O
check64 in BASE64.C.O test64 in Base64.c.o
... Chunk_alloc in CHUNK.C.O
[root@node56 lib]#
The above is a small set to introduce the Linux AR command instructions and examples of the use of explanations, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!