About the operation of static library and dynamic library

Table of contents

static library

dynamic library

1. Add .o file:

2. Delete the .o file:

View function names in static and dynamic libraries

1. View the function names in the static library (take libexample.a as an example):

2. View the function names in the dynamic library (take libexample.so as an example):

Check whether the dynamic library and static library are 32-bit or 64-bit

1. If it is a dynamic library, you can use: file *.so  

2. If it is a static library, you can use objdump -x *.a


static library

To add a .o file to a static library, the following steps are required:

  1. Use the ar command to create a static library file, such as libexample.a:

    ar rcs libexample.a example1.o
    

    This command will create a static library file libexample.a and add the example1.o file to it.

  2. If you need to add a new .o file, you can use the following command:

    ar rcs libexample.a example1.o example2.o
    

    This command will add the example2.o file to libexample.a.

  3. If you need to delete a .o file, you can use the following command:

    ar d libexample.a example1.o
    

    This command will delete the example1.o file from libexample.a.

        Or you can use the following command:

ar -x libexample.a
rm example1.o
ar rcs libexample.a *.o

First use ar -x to decompress the static library, then delete the corresponding .o files, and then repackage the rest of the .o files

dynamic library

1. Add .o file:

If you need to add a new .o file to the so library, you can compile the new source file into a .o file, and then use the 1d command to link multiple .o files into a new .so library file, for example:

gcc -c -fPIC -o new file.o new file.c
ld -shared -o new lib.so old lib.so new file.o

Among them, the -c option means compiling into an object file, the -fPIC option means compiling into position-independent code, and the -shared option means linking into a shared library. In this way, the new .o file can be added to the original .so library file


2. Delete the .o file:

If you need to delete a .o file in the so library, you can use the ar command to extract all the .o files in it, then delete the unnecessary .o files, and finally repackage the remaining .o files into a new .o file. so library files, for example:

ar -x old lib.so
rm old file.o
gcc -shared -o new lib.so *.o

Among them, the -x option means to extract all .o files, and the -shared option means to relink into a shared library

View function names in static and dynamic libraries

You can use the following commands to view function names in static and dynamic libraries:

1. View the function names in the static library (take libexample.a as an example):

nm libexample.a

This command will list all symbols in libexample.a, including function names, variable names, etc.

2. View the function names in the dynamic library (take libexample.so as an example):

nm -D libexample.so

This command will list all exported function names in libexample.so. Please note that there are still some unexported functions and variables in the dynamic library. These unexported symbols can be viewed with the following command:

nm libexample.so

This command will list all symbols in libexample.so, both exported and not exported.

T class: It is a function defined in the library, represented by T, which is the most common;    
U class: It is called in the library, but not defined in the library (indicating that other libraries are required), represented by U;    
W class: the so-called "weak state" symbols, although they are defined in the library, but may be overwritten by symbols of the same name in other libraries, represented by W.

For example: nm *.so | grep printf (whether printf is referenced in *.so)

Check whether the dynamic library and static library are 32-bit or 64-bit

1. If it is a dynamic library, you can use: file *.so  

Check whether the dynamic library libexample.so is 32-bit or 64-bit:

$ file libexample.so
libexample.so: ELF 32-bit LSB shared object, Intel 80386, version 1 (SYSV), dynamically linked, B

2. If it is a static library, you can use objdump -x *.a

objdump is a disassembler that can be used to view various information of binary files, including code segments, data segments, symbol tables, relocation tables, dynamic symbol tables, and more. The objdump -x command is used to display all header information of binary files.

Specifically, the objdump -x command will list various header information of binary files, including file type, system architecture, entry address, segment table, symbol table, dynamic symbol table, relocation table, etc. This information is very useful for debugging and analyzing binaries, and can help developers better understand the structure and runtime behavior of binaries.

It should be noted that the objdump -x command only displays the header information of the binary file. If you need to view more detailed disassembly information, you can use other objdump commands, such as the objdump -d command.

Guess you like

Origin blog.csdn.net/L888666Q/article/details/131009264