Analysis and use of static link libraries in Linux

Source: Internet
Author: User
Article Title: analysis and use of static link libraries in Linux. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.

At the C language level, code reuse is usually implemented through libraries. In the traditional sense, a library refers to a file ending with the suffix.. Strictly speaking, the function library should be divided into two types: static Link Library and dynamic link library, also known as dynamic sharing library. A static Link Library is usually a file suffixed with. a, while a dynamic link library is often suffixed with. so.

The static Link Library archives one or more target files (The. o files generated by compilation) in one file. Then, when you need to use a function in the static library, link the static library with the application to be generated.

On the Linux platform, the most commonly used archive tool is GNU tar. But to build a static library, tar is not used, but ar is another tool. Tar and ar are both archive tools, but they have different purposes. Tartar is used to create a archive file (that is, a file with a suffix of .tar). ar also completes the above work. However, it performs some additional processing to create an index for the symbols in the archive target file, when linked to an application, these indexes are recycled.

Ar is used to Package Multiple Target files into one archive file. Each target file is a member of the archive file. Various attributes of the archived file, such as the file content. Access permission. The timestamp, owner ID, and other information are also stored in the archive file. When the original target file is extracted from the archive file, these attributes can also be restored. In addition to creating and extracting, you can also append an archive file. Modify and delete operations. Ar command syntax usage:

Ar [-] p [mod] [membername] [count] archive files...

In the preceding command line syntax, archive indicates the name of the archive file. For example, you can specify it as libavi. a. archive files refers to the archive members to be operated on. There can be multiple archive files. The p on the ar command line refers to the following characters, which specifies what operations the ar performs.

D: delete a module from the database. Specify the module to be deleted based on the original file name of the module. If any option v is used, each deleted module is listed.

M: This operation is a member moving in a library. If several modules in the library have the same symbolic definition (such as function definition), the order of member positions is very important. If no option is specified, any specified member will be moved to the end of the database. You can also use the 'A', 'B', or 'I' option to move to the specified position.

P: displays the specified members in the database to the standard output. If any option v is specified, the member name is displayed before the content of the output member. If no member name is specified, all files in the database are displayed.

Q: Fast append. Add a new module to the end of the database. It does not check whether a replacement is required. The 'A', 'B', or 'I' option does not affect this operation. The module always appends the end of the library. If any option v is used, each module is listed. The symbol table of the library is not updated. You can use 'ar s' or ranlib to update the index of the symbol table of the library.

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.

T: displays the list of modules in the database. Generally, only the module name is displayed.

X: extract a member from the database. If the module to be extracted is not specified, all modules in the library are extracted.

After the operation mode options of the ar tool, you can keep up with some modifier options, namely the mod in the command line syntax, which further controls the ar behavior. The main modifier options include the following. Note: There is no space or other separator between them and the operation mode options.

A: Add a new file to an existing member of the database. If you use any option a, you should specify an existing member name for the membername parameter in the command line.

B: Add a new file before an existing member of the database. If option B is used, an existing member name should be specified for the membername parameter in the command line.

C: Create a database. Whether or not the database exists will be created.

F: truncates the specified name in the database. By default, the file name length is unrestricted. You can use this parameter to shorten the file name to ensure compatibility with other systems.

I: Add a new file before an existing member of the database. If any option I is used, you should specify an existing member name (similar to any option B) for the membername parameter in the command line ).

L: Not used yet

N: used with the count parameter to specify the number of extracted or output files when multiple identical file names exist in the database.

O: when a Member is extracted, the original data of the member is retained. If this option is not specified, the extracted module time is marked as the extracted time.

P: use the full path name for file name matching. Ar cannot use full path names when creating a library (such library files do not comply with POSIX standards), but some tools are acceptable.

S: Write a target file index to the database, or update an existing target file index. This action is even performed for libraries that have not changed. Doing ar s for a library is equivalent to doing ranlib for the library.

S: do not create the target file index, which can speed up the time when creating a large database.

U: Generally, the command ar r... insert all listed files to the database. If you only want to insert files in the list file that are new to files with the same name as the files in the database, you can use this option. This option is only used for r operation.

V: This option is used to display additional information about the operation options.

V: displays the ar version.

The above many options are commonly used with three Command Options: r (insert), c (create), and s (create index ), these three options are often used together. Assume that there are two C files: foo. c bar. c. Compile foo. c and bar. c as the target file foo. o and bar. o, and archive the two files as a static Link Library.

// Bar. c

# Include "foobar. h"

Char * bar (void)

{

Printf ("This is bar! Library1 is called \ n ");

Return ("bar ");

}

// Foo. c

# Include "foobar. h"

Char * foo (void)

{

Printf ("This is foo! Library2 is called! \ N ");

Return ("foo ");

}

// Foobar. h

# Ifndef _ FOOBAR_H _

# Define _ FOOBAR_H _

# Include

# Include

# Include

Extern char * foo (void );

Extern char * bar (void );

# Endif

# Gcc-c-o foo. o foo. c

# Gcc-c-o bar. o bar. c

# Ar rcs libfoobar. a foo. o bar. o

This is based on the PC platform. For the construction of static link libraries on the embedded platform, the process is the same. The only change may be the name of the tool used. For example, if you want to build a static library for ARM-Linux, you may need to use arm-linux-ar. Another tool here is nm, which can be used to obtain the symbol information of the target file. Here, nm prints two symbols in libfoobar. a: foo and bar. These two symbols represent functions, so their symbol values are 0 and the symbol type is T (text, indicating that the symbol is in the code segment ). The last column is the name of the symbol.

# Nm libfoobar.

Foo. o:

0000000000000000 T foo

U puts

Bar. o:

0000000000000000 TB bar

U puts

The current static library is available. How can I use such a static library. To use a static library, the application must be linked to the static library. Suppose there is a c file of main. C. The link between the application and the static library is completed during the compilation period.

# Gcc-g-o foobar main. c-L.-lfoobar

Zfz @ zfz :~ /Program $./foobar

This is foo! Library2 is

Foo () = foo

This is library1 is called

Bar () = bar

-L. The option tells the linker to go down to the current directory to find the static library to be connected, and-lfoobar clearly tells the linker to complete the link with the static library libfoobar.. Here there must be a space between-L. and the following. You can also Copy the generated file to the/usr/lib directory, which is the default directory searched by the system.

A static Link Library is a copy-based link process. What is a "replicaset" link process? When a static Link Library is connected to an application, the linker copies the static Link Library to the final executable code. For example, there are two applications A and B, both of which must use the functions provided by libfoobar.. Then, when compiling link A, the linker will copy A libfoobar. the final executable code from a to A goes to libfoobar. the debugging information in a is also copied. Similarly, when you connect to B, the linker also copies a libfoobar. the final executable code from a to B goes through. This is the meaning of the "replicaset" link.

Related Article

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.