ZT: I used the Linux command AR-Create a static library. A file

Source: Internet
Author: User
Tags base64

I used the Linux command AR-Create a static library. A file

This article link: http://codingstandards.iteye.com/blog/1142358 (reproduced please specify the source)

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. (Supplemental: If you do not need to create an index, you can change it to uppercase s parameter; if the. A file is missing an index, you can add it 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 example one in a shell script

Bash code
    1. os= ' Uname-r '
    2. Ar RCS libhycu.a. $OS *.O

Example two using the Makefile code in makefile
    1. $ (BIN1): $ (BIN1_OBJS)
    2. 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: Create a static library from the. o file.

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 run independently of the. a file.

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: Create a dynamic library file from an. o 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]#

Problem thinking related information

"1" CSDN Document Center Linux Library Operations Command AR and NM

"2" Linux AR package Library to another library [go]

"3" My embedded design home analysis of Linux static libraries and dynamic libraries

Use of the "4" Linux Treasury AR and NM commands

ZT: I used the Linux command AR-Create a static library. A file

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.