(5) gcc compiler 2. Making of static library and dynamic library

Summary of content:
      This section mainly introduces the production and use of static and dynamic libraries, and compares the advantages and disadvantages of static and dynamic libraries


One, the production and use of static libraries

      A static library is a file ending in .a in Linux. Static libraries are used when generating executable files when linking during compilation. The naming rule of the static library is lib+(libname)+.a, for example, libmytest.a

Production steps:

Features command
1. Generate .o file gcc -c hello.c -o hello.o
2. Pack the .o file ar rcs libmytest.a *.o

The use of static libraries:

There are two methods that need to be mastered.

gcc 源文件 静态库 -o 输出文件名
gcc mian.c libmytest.a -o myapp
gcc 源文件 -L 库目录 -l 静态库的名字,不加lib的 -o 输出文件名
gcc mian.c -L lib -l mytest -o myapp

You can use the nm command to view the .o files in the static library.

nm libmytest.a

Advantages and disadvantages of static libraries

Using a static library is to add the .o file in the static library to the executable program when generating the executable program.
Advantages: There
is no need to link again when running the program, it runs fast, and there is no need to provide corresponding libraries when publishing the program.

Disadvantages: The
executable program is relatively large, and the program needs to be recompiled every time the library is upgraded.

Second, the production and use of dynamic libraries

      The dynamic library is a file ending with .so in linux. The dynamic library is only linked when the executable file is running. The naming rule of the dynamic library is lib+(libname)+.so, for example, libmytest.so

Production steps:

Features command
1. Generate position-independent .o files gcc -fPIC -c hello.c -o hello.o
2. Pack the .o file gcc -shared -o libmytest.so *.o

Use of dynamic library:

There are two methods that need to be mastered.

gcc 源文件 动态库 -o 输出文件名
gcc mian.c libmytest.so -o myapp
gcc 源文件 -L 库目录 -l 动态库的名字,不加lib的 -o 输出文件名
gcc mian.c -L lib -l mytest -o myapp

You can use the ldd command to view the shared libraries that the executable file depends on.

ldd myapp

Advantages and disadvantages of dynamic libraries

The executable file generated by the dynamic library needs to be linked to the dynamic library at runtime.
Advantages: The
executable program is small, and there is no need to recompile each library update.

Disadvantages: It
runs slowly, and the corresponding library must be provided when the program is released.

Three, specify the location of the shared library

The linker cannot find the location of the shared library and needs to configure environment variables.
There are two methods
(1) Temporarily use, generally for testing, assign LD_LIBRARY_PATH to the address of the shared library.

export LD_LIBRARY_PATH = ./lib

(2) Used for permanent setting
1. Modify the configuration file of the dynamic linker.

vi /etc/ld.so.conf

2. Add the absolute address of the dynamic library in ld.so.conf

/home/itcast/calc/lib

3. Update the ld.so.conf file

sudo ldconfig -v

It's just some related commands for making dynamic and static libraries, let's take a look when they are used.

Guess you like

Origin blog.csdn.net/qq_23844501/article/details/113844433