Instructions for use of the "Turn" Linux ar command that and example

Source: Internet
Author: User
Tags base64

from:http://blog.csdn.net/xljiulong/article/details/7082960Description of the use of the Linux AR command that and exampleTags: linuxmakefilegccarchivecodecshell2011-12-19 09:14 5409 People read comments (0) favorite reports Description of Use
Create a static library. A file. It is often used when developing programs in C/D + +, but I rarely use AR commands on the command line alone, usually in makefile, and sometimes in shell scripts. For knowledge about library files, static libraries, dynamic libraries, and how to create and use Linux, see the related data "3", "about Linux static and dynamic Libraries" later in this article.
Common parameters
Format: ar RCS libxxx.a xx1.o xx2.o
Parameter r: Insert module (replace) in library. When the inserted module name already exists in the library, replace the module with the same name. If there is a module in several modules that 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. It is created regardless of whether the library exists.
Parameter s: creates the target file index, which can speed up time when creating a large library. (add: If you do not need to create an index, you can change the uppercase s parameter; A file is missing an index and can be added using the Ranlib command)
Format: AR t libxxx.a
Shows which target 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 details such as file name, time, size, and so on.
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 example
Example one used in a shell script
Bash code
os= ' Uname-r '
Ar RCS libhycu.a. $OS *.O
Example two used in makefile
Makefile Code
$ (BIN1): $ (BIN1_OBJS)
AR RCS [email protected] $^
Example three creating and using a static library
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, 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 the 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
Fourth step: 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. A file runs.
Fifth step: Execute.
./main
Example four creating and using a dynamic library
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, 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
Fourth step: Use the dynamic library in your program.
Gcc-o main 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.
Fifth step: Execute.
Ld_library_path=. ./main
Example five viewing files in a static library
[Email protected] lib]# ar-t libhycu.a
base64.c.o
binbuf.c.o
cache.c.o
chunk.c.o
codec_a.c.o
...
xort.c.o
[Email protected] lib]#
[Email protected] lib]# Ar-tv libhycu.a
rw-r--r--0/0 7220 Jul 19:18 BASE64.C.O
rw-r--r--0/0 2752 Jul 19:18 BINBUF.C.O
rw-r--r--0/0 19768 Jul 19:18 CACHE.C.O
...
rw-r--r--0/0 4580 Jul 19:18 XORT.C.O
[Email protected] lib]#
[Email protected] 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
[Email protected] lib]#

Instructions for use of the "Turn" Linux ar command that and example

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.