How to package a static library. A file (ar,ranlib,nm command Introduction)

Source: Internet
Author: User

A common script

1 Packaging scripts

The script is as follows, with the AR and ranlib Command Reference (command from the network)

Alllib=*.a
file= ' ls *.a '
#原来的库解压重命名

For F in $FILE
Do
AR x $F
obj= ' ar t $F '
For O in $OBJ
Do
MV $O ${f}_${o}
Done
Done

#ar C creates a library, AR r inserts the file. AR s ==ranlib inserting files into the library or updating the library

AR cr $ALLLIB *.O
Ranlib $ALLLIB
MV $ALLLIB: /

MKDIR-P tmp
MV *.O TMP

2 Updating the static library

Using AR R

3 Merging static libraries

AR advanced Usage---use AR script

The first step:
We enter it once in the command terminal
echo CREATE libyuerapi.a > Ar.mac Enter
echo SAVE >> Ar.mac Enter
echo END >> Ar.mac return
Ar-m < Ar.mac

We can see the contents of the Ar.mac file in a cat Ar.mac, and we can see that there is a LIBYUERAPI.A generated. At present there is nothing in it.

Step Two:
The previous step we have successfully created the LIBYUERAPI.A file, and now we add the. o File to it
Ar-q LIBYUERAPI.A YUER1.O
Ar-q LIBYUERAPI.A YUER2.O
Ar-q LIBYUERAPI.A YUER3.O

Step Three:
Add LIBYUCOM.A to the LIBYUERAPI.A library file
We create a Ar.mac file in the same way
echo OPEN libyuerapi.a > Ar.mac Enter
echo addlib libyucom1.a >> Ar.mac Enter
echo SAVE >> Ar.mac Enter
echo END >> Ar.mac return
Ar-m < Ar.mac Enter

Two specific instructions

When our program has frequently used modules, and this module is also used in other programs, at this time according to the idea of software reuse, we should create libraries, so that later programming can reduce the amount of development code. Here are two commands ar and NM, which are used to manipulate the library.

AR Basic usage

When our program has frequently used modules, and this module is also used in other programs, at this time according to the idea of software reuse, we should create libraries, so that later programming can reduce the amount of development code. Here are two commands ar and NM, which are used to manipulate the library.

The AR command can be used to create, modify, or present a single module from a library. A library is a separate file that contains other files that are organized according to a specific structure (called the member of this library file). The content, mode, timestamp, owner, group, and other attributes of the original file are preserved in the library file.

The following is the format of the AR command:

AR [-]{DMPQRTX}[ABCFILNOPSSUVV] [membername] [count] archive files ...

For example we can use ar rv libtest.a hello.o hello1.o to generate a library, the library name is test, link can be used-ltest link. The library contains two modules hello.o and HELLO1.O. You can have the '-' character before the option or not. Let's take a look at the command's action options and any options. Now we refer to the {DMPQRTX} section as the action option, while the [ABCFILNOPSSUVV] section is called an option.

The action options in {DMPQRTX} are only available in the command and must use one of them, meaning the following:

    • D: Remove the module from the library. Specifies the module to be deleted by the original file name of the module. If any option V is used, each module that is deleted is listed.
    • M: This action is to move a member in a library. When a library has several modules with the same symbolic definition (such as a function definition), the position order of the members is important. If no option is specified, any specified member is moved to the end of the library. You can also use the ' a ', ' B ', or ' I ' option to move to the specified position.
    • P: Displays the members specified in the library to the standard output. If you specify any option V, the name of the member is displayed before the content of the member is output. If you do not specify the name of the member, all files in the library will be displayed.
    • Q: Quick append. Add a new module to the end of the library. Does not check if replacement is required. The ' A ', ' B ', or ' I ' options have no effect on this operation, and the module is always appended at the end of the library. If any option V is used, each module is listed. At this point, the library's symbol table is not updated, you can use ' ar s ' or ranlib to update the library's symbol table index.
    • R: Insert the module (replace) in the 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.
    • T: Displays a list of module tables for the library. Typically only the module name is displayed.
    • X: Extracts a member from a library. If you do not specify a module to extract, all modules in the library are extracted.

Here's a look at any option that can be used in conjunction with the action options:

    • A: Add a new file after an already existing member of the library. If you use either option A, you should specify a member name that already exists for the membername parameter in the command line.
    • B: Add a new file in front of an already existing member of the library. If you use either option B, you should specify a member name that already exists for the membername parameter in the command line.
    • C: Create a library. It is created regardless of whether the library exists.
    • F: Truncate the specified name in the library. By default, the file name length is unrestricted, and you can use this parameter to truncate the file name to ensure compatibility with other systems.
    • I: Add a new file in front of an already existing member of the library. If you use either option I, you should specify a member name that already exists for the membername parameter in the command line (similar to option B).
    • L: not currently used
    • N: Used with the count parameter to specify the number of extracts or outputs when there are multiple identical filenames in the library.
    • O: Retains the original data of the member when the member is extracted. If you do not specify this option, the time of the extracted module is marked as the extracted time.
    • P: Use full pathname when file name matches. AR cannot use the full pathname when creating the library (such a library file is not POSIX compliant), but some tools can.
    • S: Writes a target file index into the library, or updates an existing target file index. This action is even done for libraries that do not have any changes. Doing AR s for a library is equivalent to doing ranlib to the library.
    • S: Do not create a target file index, which can speed up time when creating large libraries.
    • U: Generally speaking, command ar R ... Insert all listed files into the library, and you can use this option if you want to insert only those files that are listed in the file that are newer than the library file name. This option is available only for the R operation option.
    • V: This option displays additional information for the option to perform the operation.
    • V: Displays the version of AR.

For each symbol, NM lists its value (the symbol value), the type (the symbol type), and its name (the symbol name).

For example

For each symbol, NM lists its value (the symbol value), the type (the symbol type), and its name (the symbol name). The following example:
00000024 T Cleanup_before_linux
00000018 T Cpu_init
00000060 T dcache_disable
00000054 T dcache_enable
0000006c T Dcache_status
00000000 T Do_reset
0000003c T icache_disable
00000030 T icache_enable
00000048 T Icache_status
The above display is the output using NM CPU.O, for cleanup_before_linux this symbol, 00000024 is the value of the 16 binary display, T is its type, and Cleanup_before_linux is its name. As you can see, the value of the symbol shown above is actually the offset of the function in the text section of the Cleanup_before_linux. However, the specific meaning of each symbol's value varies depending on its type. Of course, for each symbol's value, its type, its value, and the section to which they belong are closely related.

Summarize:

Symbol type Description
A The value of the symbol is absolute and is not allowed to be changed during subsequent links. Such symbolic values often appear in interrupt vector tables, such as symbols that represent the position of individual interrupt vector functions in the interrupt vector table.
B The value of the symbol appears in the non-initialized data segment (BSS). For example, define a global static int test in a file. The type of the symbol test is B and is in the BSS section. Its value represents the offset of the symbol in the BSS segment. Generally, BSS segments are allocated in RAM
C The symbol is common. Common symbol is an uninitialized session data segment. The symbol is not included in a common section. Allocations are only made during the link process. The value of the symbol represents the number of bytes required for the symbol. For example, in a C file, the int test is defined, and the symbol is referenced elsewhere, and the symbol type is C. Otherwise, its type is B.
D The symbol is in the initial session data segment. In general, it is assigned to the data section. For example, defining global int baud_table[5] = {9600, 19200, 38400, 57600, 115200} is assigned to the initialization data segment.
G The symbol is also in the initialization data segment. Used primarily for small object to improve access to small data object in one way.
I The symbol is an indirect reference to another symbol.
N The symbol is a debugging symbol.
R The symbol is located in the read-only data area. For example, define Global const int test[] = {123, 123}, then test is the symbol for a read-only data area. Note that the test in the source file corresponds to _test and its symbol type is D, which is initialized in the data segment, if Cygwin is compiled directly into MZ format using GCC. However, if you use a cross-compilation tool such as M6812-ELF-GCC, the test in the source file corresponds to the test of the target file, which is not underlined, and its symbol type is R. Generally, it is located in Rodata section. It is worth noting that if const char *test = "ABC" is defined in a function, const char Test_int = 3. No symbolic information is obtained using NM, but the string "ABC" is assigned to read-only memory, and test is in the Rodata section with a size of 4.
S The symbol is located in the non-initialized data area for small object.
T The symbol is located in the Code area text section.
U The symbol is undefined in the current file, meaning that the symbol is defined in another file. For example, the current file invokes a function defined in another file, where the called function is currently undefined, but the type in the file that defines it is t. But for global variables, in the file that defines it, the symbol type is C, and in the file that uses it, the type is U.
V The symbol is a weak object.
W The symbol is a weak symbol, the have not been specifically tagged as a weak object symbol.
- The symbol is stabs symbol in the a.out format file.
? The symbol type is not defined

Updating the symbol Index table for a static library

The content of this section is relatively simple. As mentioned before, static library files need to be created and maintained using "AR". When you add a member to a static library (adding an. o file to a static library), AR can simply append the. o file that needs to be added to the end of the static library. Then, when we use this library for the connection generation executable, the linker "LD" prompts an error, which may be: The main program uses a function or global variable defined in the. o file that was previously added to the library, but the connector cannot find the function or variable.

The reason for this problem is that the. o file that we compiled before is added directly to the end of the library, but does not update the library's valid symbol table. When the connector is connected, the function or variable defined in the. o file that you just joined cannot be located in the Symbol Index table of the static library. This requires that the functions (variables) defined in all. o files that are added after the library member is appended are valid, and that this work requires the use of another tool "Ranlib" to update the symbol Index table of the static library.

In the static library (document file) We use, there is a special member whose name is "__." Symdef ". It contains valid symbols (function names, variable names) defined by all members in the static library. Therefore, when you add a member to the library, you need to update the member "__." Symdef ", otherwise all symbols defined in the added member will not be positioned by the connecting program. The command to complete the update is:
Ranlib Archivefile
Usually in makefile we can do this:
LIBFOO.A:LIBFOO.A (X.O) libfoo.a (y.o) ...
Ranlib LIBFOO.A

It does this by updating the static library members "X.O" and "y.o" to the members of the static library "__." Symdef "Update (update the library's symbol Index table).

If we use the GNU AR Tool to maintain and manage static libraries, we do not need to consider this step. GNU AR itself has provided the ability to update the symbol Index table while updating the library (this is the default behavior, or you can control the specific behavior of AR with command-line options.) Refer to the GNU AR Tool's Man manual).

The GNU Tools AR is used to make library files. A, but also provides a ranlib, from the manual to see Ranlib equivalent to Ar-s, why so?

This is because the first AR program on a UNIX system is simply used to package multiple. O to. A (similar to what tar does) without processing the symbol table in the. O. The linker program requires a. A file to provide a complete symbol table, so a separate Ranlib program was written to produce the symbolic information needed to generate the linker. In other words, producing a linker qualified. A file requires two steps of AR and ranlib.

Soon, the UNIX vendors found that ranlib do things can be integrated into AR, so the AR program upgrade version includes the function of Ranlib, But the makefile of many early projects have been generated in a two-step way. A, so in order to ensure the compatibility of these early files, Ranlib was preserved.

Now, Gnu/linux system, Ranlib still exist, of course, most of the projects have not used it, because Ar-s did ranlib work.
History is usually a mix of progress and compromise!

How to package a static library. A file (ar,ranlib,nm command Introduction)

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.