Linux command ar-Create a static library. A file
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
Dynamic libraries can also use the "Specify Dynamic Library search path in profile/etc/ld.so.conf" In addition to the default path/lib and/usr/lib, the way of environment variables, and "Specify the dynamic library search path for the program when compiling the target code (through GCC parameters"-WL, -rpath, "designated" three species, their priority is also different;
The second type is described below:
For example:
# Export Ld_library_path=.
However, in order to facilitate the example, this article uses another method of setting the environment variable, which is not only valid for the command but also for the environment variable, which is not valid when the command completes. As the following order:
# ld_library_path=. ./main
example five viewing 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
...
xort.c.o
[Root@node56 lib]#
[Root@node56 lib]# Ar-tv libhycu.a
rw-r--r--0/0 7220 June 19:18 BASE64.C.O
rw-r--r--0/0 2752 June 19:18 BINBUF.C.O
rw-r--r--0/0 19768 June 19:18 CACHE.C.O
...
rw-r--r--0/0 4580 June 19:18 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]#
This article is from Black Network-China's largest network security site link: http://www.hackbase.com/tech/2011-08-09/64867.html