Basic methods for compiling static libraries (. A) and dynamic libraries (. So) in Linux

Source: Internet
Author: User

Static Library

In Linux, use the AR command to create a static library file. The following is a command option:

D ----- delete an object from the specified static library file

M ----- move the file to the specified static library file

P ----- output the specified file in the static library file to the standard output

Q ----- quickly append a file to a static library file

R ----- Insert the file into the static library file

T ----- display the list of files in the static library file

X ----- Extract files from static library files

There are also multiple modifiers to modify the above basic options. For details, see man AR below to list three:

A ----- after adding the new target file (*. O) to the static library file

B -----************************************** * Before

V ----- use the verbose mode

The command line format of the Ar command is as follows:

Ar [-] {dmpqrtx} [abcfilnopssuvv] [membername] [count] archive files...

The parameter archive defines the name of the library. Files is a list of target files contained in the library file. Each file is separated by spaces.

For example, the command for creating a static library file is as follows:

Ar-r libapue. A error. O errorlog. O lockreg. o

In this way, the libapue. A static library file can be displayed with the T option.

After creating the library file, you can create the index of the static library file to help increase the compilation speed of other programs connected to the library. use the ranlib program to create a database index, which is stored in the database file.

Ranlib libapue.

Display the index of the archive file with the NM program, which can display the symbols of the target file

NM libapue. A | more

If it is to display the symbols of the target file:

NM error. o | more

How to use it? As follows:

Gcc-O test. c libapue.

In this way, the function in libapue. A can be called in test. C.

Dynamic library

1. Create a shared library

Gcc-shared-O libapue. So error. O errorlog. o

In this way, a shared library is created!

2. Compile the Shared Library

Assume that the shared library is located in the current directory (that is, the same directory as the program file)

Gcc-O test-L.-lapue test. c

This will compile the executable file that does not contain the function code, but you will find that the Linux dynamic loader does not have the libapue. So file.

You can run the LDD command to view the shared libraries that the executable files depend on:

LDD Test

How can the dynamic loader discover library files? There are two solutions:

LD_LIBRARY_PATH environment variable

/Etc/lD. So. conf file

1. Environment Variables

Export LD_LIBRARY_PATH = "$ LD_LIBRARY_PATH :."

2. Modify the/etc/lD. So. conf file. The file is located in/etc/lD. So. conf.

Generally, the library file of an application is not in the same directory as the system library file. Generally, the shared library file of the application is stored under/usr/local/lib, create a new directory named apue, and run libapue. so just copy it.

At the same time, add a line in/etc/lD. So. conf:

/Usr/local/lib/apue

In the future, add the compilation options when compiling the program:

-L/usr/local/lib/apue-lapue

In this way, you can use this libapue. So shared library !!

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.