Gnu ar, ranlib, and Nm

Source: Internet
Author: User

Reference: http://study.chyangwa.com/IT/AIX/aixcmds1/ar.htm#a0949a5b

From: http://hi.baidu.com/doyee/blog/item/ad4c4a341089ed47251f1484.html

From: http://hi.baidu.com/kkernel/blog/item/e729cffa9036318e9f514655.html/cmtid/fa138acfe86e5533f8dc6183

 

Role of ranlib:

Cc =/usr/local/ndk/toolchain/ARM-Eabi/bin/ARM-Eabi-gcc
LD =/usr/local/ndk/toolchain/ARM-Eabi/bin/ARM-Eabi-gcc
AR =/usr/local/ndk/toolchain/ARM-Eabi/bin/ARM-Eabi-ar
Ranlib =/usr/local/ndk/toolchain/ARM-Eabi/bin/ARM-Eabi-ranlib

Cflags =-March = armv5te/-msoft-float/-FPIC/-mthumb-interwork/-O2/-dammo_video_player
Cxxflags =-March = armv5te/-msoft-float/-FPIC/-mthumb-interwork/-O2/-fno-exceptions/-fno-rtti
Outdir =.../../makefile/Linux/Android/

 

//////////////////////////////////////// //////////////////////////////////////// //////////////////////////////////////

When compiling a static library in Linux, ar does not include any options. after file a, what is the use of the ranlib operation on it? If you do not perform the ranlib operation, what are the consequences? I can run it normally without using this step during compilation. Please explain it to me. Thank you.

 

Reply:

Http://www.linuxsir.org/main/doc/gnumake/GNUmake_v3.80-zh_CN_html/make-11.html

Update the symbol index table of the static library

This section is relatively simple. As mentioned above, static library files must be created and maintained using "Ar. When a member is added to the static database (a. o file is added to the static database), "Ar" can be used directly.
Append the. o file to the end of the static library. When we use this library to connect to generate executable files, the linked program "LD" prompts an error. This may be because the main program uses
Previously added a function or global variable defined in the. o file in the library, but the connection program cannot find this function or variable.

The reason for this problem is that we added the compiled. o file directly to the end of the database, but did not update the effective symbol table of the database. When the connection program is connected, it cannot be specified in the symbol index table of the static library.
The function or variable defined in the. o file just added. This requires that the function (variable) defined in all the added. O files be valid after the database member append is completed.
Tool "ranlib" to update the symbol index table of the static library.

The static library (document file) We use has such a special member, whose name is "_. symdef ". It contains the valid symbols (function name,
Variable name ). Therefore, when a member is added to the database, the corresponding member "_. symdef ", otherwise all the symbols defined in the added member will not be located by the Connection Program. Complete
The update command is:

Ranlib archivefile

In makefile, we can do this:

Libfoo. A: libfoo. A (x. o) libfoo. A (Y. O )...

Ranlib libfoo.




It updates the static library member "x. O and Y. O ", the static library member" _. symdef "to update (update the symbol index table of the database ).

If we use the GNU ar tool to maintain and manage static libraries, we do not need to consider this step. GNU ar itself provides the ability to update the symbol index table while updating the Library (this is the default behavior, you can also use the command line option to control the specific behavior of AR. See the man manual of the GNU ar tool ).


 

 

In GNU, Ar is used to create the library file. A, but also provides a ranlib. From the manual, ranlib is equivalent to ar-S. Why?

This is because the earliest ar program on UNIX systems is simply used to Package Multiple. O to. A (something similar to tar) without processing the symbol table in. O. Linker program requires
The. A file provides a complete symbol table, so a separate ranlib program was written at that time to generate the symbol information required by linker. That is to say, a linker
The. A file of lattice requires two steps: Ar and ranlib.

Soon, Unix vendors discovered that ranlib could be fully integrated into Ar, so the upgraded version of the Ar program included the ranlib function, however, many early project makefiles were generated in two steps. a, so to ensure the compatibility of these early files, ranlib is retained.

Now, ranlib still exists in GNU/Linux systems. Of course, most projects do not use it anymore, Because ar-S has done ranlib.
History is usually a mixture of progress and compromise!

1. Basic ar usage

The ar command can be used to create and modify a database, or to propose a single module from the database. The library is a separate file that contains other files organized according to a specific structure (called the member of the library file ). Attributes of the original file, such as content, mode, timestamp, owner, and group, are kept in the library file.

The format of the Ar command is as follows:
Ar [-] [abcfilnopssuvv] [membername] [count] archivefiles...

For example, we can use ar rvl ibtest. A hello. o
Hello1.o is used to generate a library named test, which can be linked with-ltest. The library contains two modules: Hello. O and hello1.o. You can have
'-' Character, or no. Next let's take a look at the operation options and any options of the command. Now we call part of the operation option, while the part of [abcfilnopssuvv] is called any option.

The operation options in can only use one of them in the command. Their meanings are as follows:
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. Do not check whether 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 'ars' 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.

The following describes any options that can be used with operation 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 the ARS for a library is equivalent to doing the 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 ARR command... 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.

Ar common options:

Ar RCS libmylib. A mylib. o


Ranlib


[

-T


] [
-X
{32
|64
|32_64
}]


Archive...

Description

Ranlib




Command

SetArchive
Database to random database. A random library is an archive library that contains symbol tables.

If-T
Option,Ranlib



Command

Only archive is mentioned, but they are not modified. After copying an archive, or to avoidLD
Command

Used to Display error messages about the expired symbol tableMake
Command

Of-T
This is very useful.

Flag


-T
Mentioned the specified archive without modifying them.

-X
Mode

SpecifyRanlib


Type of the target file to be checked.Mode
Must be one of the following:

32
Only 32-bit target files are processed
64
Only processing 64-bit target files
32_64
Process 32-bit and 64-bit target files

By default, 32-bit target files are processed (64-bit target files are ignored ).Mode
AndObject_mode
Set environment variables together. For example,Object_mode = 64
MakeRanlib


Process any 64-bit target file and ignore the 32-bit target file.-X
The flag overwritesObject_mode
Variable.

Example

Archive files to be randomizedGenlib.

, Enter:

RanlibGenlib.

3. Basic usage commands of Nm



Nm is used to list the symbols of the target file. The following is the format of the NM command:



NM [-A | -- debug-Syms] [-G | -- extern-only] [-B] [-c | -- demangle]
[-D | -- Dynamic] [-S | -- print-armap] [-o | -- print-file-name]
[-N | -- Numeric-Sort] [-p | -- no-Sort] [-r | -- reverse-Sort]
[-- Size-Sort] [-u | -- undefined-only] [-L | -- line-numbers]
[-- Help] [-- version] [-T Radix | -- Radix = Radix] [-p | -- portability] [-F
Format | -- format = format] [-- target = bfdname] [objfile...]



If the target file is not specified for the NM command, Nm assumes that the target file is a. Out. The following lists any options for this command. Most options include short formats starting with "-" And long formats starting.



-A,-o, or -- print-file-Name: add the file name before the names of the symbols found, instead of displaying the file name only once before all the symbols in the file.

For example, the output of NM libtest. A is as follows:



Cpthread. O:

00000068 t Main _ 8 cpthreadpv

00000038 t start _ 8 cpthread

00000014 T _. _ 8 cpthread

00000000 T _ 8 cpthread

00000000? _ Frame_begin __

.......................................



The output of nm-A is as follows:



Libtest. A: cpthread. O: 00000068 t Main _ 8 cpthreadpv

Libtest. A: cpthread. O: 00000038 t start _ 8 cpthread

Libtest. A: cpthread. O: 00000014 T _. _ 8 cpthread

Libtest. A: cpthread. O: 00000000 T _ 8 cpthread

Libtest. A: cpthread. O: 00000000? _ Frame_begin __

..................................................................



-A or -- debug-Syms: displays debugging symbols.

-B: equivalent to -- format = BSD, used to be compatible with MIPS nm.

-C or -- demangle: decodes a low-level symbolic name (demangle) into a user-level name. This makes the C ++ function name readable.

-D or -- Dynamic: displays dynamic symbols. This option is only valid for dynamic targets (such as shared libraries of a specific type.

-F format: Output in format. Format can select BSD, sysv, or POSIX. This option is useful in GNU nm. The default value is BSD.





 

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.