Description
Create a static library. a file. C/C ++ is often used to develop programs, but I rarely use ar commands in command lines separately. It is generally written in makefile and sometimes used in shell scripts. Knowledge about library files, static libraries, dynamic libraries, and how to create and use libraries in Linux, for more information, see [3] Linux static library and dynamic library analysis.
Common Parameters
Format: ar rcs libxxx. a xx1.o xx2.o
Parameter r: Insert a module (replace) into the database ). If the inserted Module name already exists in the Database, replace the module with the same name. If one of the 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 database. You can use other options to change the position of the added member. [1]
Parameter c: Create a database. Whether or not the database exists will be created.
Parameter s: Create the index of the target file, which can speed up the creation of a large library. (Supplement: if you do not need to create an index, you can change it to the uppercase S parameter. If the. a file lacks an index, you can use the ranlib command to add it)
Format: ar t libxxx.
Displays the target files in the library file. Only the names are displayed.
Format: ar TV libxxx.
Displays the target files in the library file, including the file name, time, size, and other details.
Format: nm-s libxxx.
Displays the index table in the library file.
Format: ranlib libxxx.
Create an index table for the database file.
Example
Example 1: Use
Bash code
OS = 'uname-R'
Ar rcs libhycu. a. $ OS *. o
Example 2 used in makefile
Makefile code
$ (BIN1): $ (BIN1_OBJS)
Ar rcs $ @ $ ^
Example 3: create and use a static library
Step 1: edit the source file test. h test. c main. c. The main. c file contains the main function as the program entry. test. c contains the functions needed in the main function.
Vi test. h test. c main. c
Step 2: compile test. c into the target file.
Gcc-c test. c
If test. c is correct, the target file test. o will be obtained.
Step 3: Create a static library from the. o file.
Ar rcs libtest. a test. o
Step 4: Use the static library in the program.
Gcc-o main. c-L.-ltest
Because it is a static compilation, the generated execution file can run independently of the. a file.
Step 5: Execute.
./Main
Example 4 create and use a dynamic library
Step 1: edit the source file test. h test. c main. c. The main. c file contains the main function as the program entry. test. c contains the functions needed in the main function.
Vi test. h test. c main. c
Step 2: compile test. c into the target file.
Gcc-c test. c
The previous two steps are the same as creating a static database.
Step 3: create a dynamic library file from the. o file.
Gcc-shared-fPIC-o libtest. so test. o
Step 4: Use the dynamic library in the program.
Gcc-o main. c-L.-ltest
When the static library and the dynamic library have the same name, the gcc command takes precedence over the dynamic library.
Step 5: Execute.
LD_LIBRARY_PATH = ../main
Example 5 view files in the static library
[Root @ node56 lib] # ar-t libhycu.
Base64.c. o
Binbuf. c. o
Cache. c. o
Chunk. c. o
Codec_a.c.o
...
Xort. c. o
[Root @ node56 lib] #
[Root @ node56 lib] # ar-TV libhycu.
Rw-r -- 0/0 7220 Jul 29 2011 base64.c. o
Rw-r -- 0/0 2752 Jul 29 2011 binbuf. c. o
Rw-r -- 0/0 19768 Jul 29 2011 cache. c. o
...
Rw-r -- 0/0 4580 Jul 29 2011 xort. c. o
[Root @ node56 lib] #
[Root @ node56 lib] # nm-s libhycu. a | less
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] #