Linux Some concepts about dynamic link libraries and static link libraries

  
 Library Introduction

The library has both dynamic and static. Dynamics usually use .so as the suffix and static as the .s. For example: libhello.so libhello.a

In order to use different versions of the library in the same system, you can add the version number to the suffix after the library file name, for example: libhello.so.1.0, due to program connection default Use .so as the file extension. So in order to use these libraries, it is common to use a way to establish symbolic connections.
ln -s libhello.so.1.0 libhello.so.1ln -s libhello.so.1 libhello.so using the library

When you want to use a static library, the connector will find the program The required functions are then copied to the executable file. Since this copy is complete, once the connection is successful, the static library is no longer needed. However, this is not the case with dynamic libraries. The dynamic library will leave a mark in the executor & lsquo; indicates that when the program is executed, the library must first be loaded. Since the dynamic library saves space, the default operation for connecting under Linux is to connect the dynamic library first, that is, if there are static and dynamic libraries at the same time, if it is not specified, it will be connected with the dynamic library.

Now suppose there is a program development package called hello, which provides a static library libhello.a a dynamic library libhello.so, a header file hello.h, the header file provides sayhello() function
/* hello.h */void sayhello();

There are also some documentation. This is a typical program development package structure

1. Connect with the dynamic library

linux default is to connect with the dynamic library, the following program testlib.c uses the sayhello() in the hello library Function
/*testlib.c*/#include#includeint main(){sayhello();return 0;}

Compile with the following command
$gcc -c testlib.c -o testlib.o

Connect with the following command:
$gcc testlib.o -lhello -o testlib

Note that when connecting, assume that both libhello.so and libhello.a are in the default library search path /usr/Under lib, if you want to add -L parameter to other locations

2. Connect with static library

It is troublesome to connect with static library, mainly parameter problem. Or the above example:
$gcc testlib.o -o testlib -WI,-Bstatic -lhello

Note: This special "-WI,-Bstatic" parameter is actually passed to the connector ld Indicates that it is connected to a static library. If there is only a static library in the system, of course, this parameter is not needed.

If you want to connect with multiple libraries, and each library is connected differently, for example, the above program should be statically connected with libhello and dynamically connected with libbye. The command should be:
$gcc testlib.o -o testlib -WI,-Bstatic -lhello -WI,-Bdynamic -lbye

3. Path problem for dynamic libraries

In order for the executable to find the dynamic library smoothly, There are three ways:

(1) Copy the library to the /usr/lib and /lib directories.

(2) Add the path of the library in the LD_LIBRARY_PATH environment variable. For example, the dynamic library libhello.so is in the /home/ting/lib directory, using bash as an example. Use the command:
$export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home/ting/lib

(3) Modify /etc/The ld.so.conf file adds the path where the library resides to the end of the file and performs an ldconfig refresh. In this way, all the library files in the added directory are visible

4. View the symbols in the library

Sometimes it may be necessary to check which functions are in a library, the nm command can print out the library. All the symbols involved. Libraries can be either static or dynamic. There are many symbols listed in nm, there are three common ones, one is called in the library, but not defined in the library (indicating that other library support is needed), represented by U; one is the function defined in the library, This is the most common with T; the other is the so-called "weak" symbols, which are defined in the library but may be covered by symbols of the same name in other libraries, denoted by W. For example, suppose the developer wants to know if printf() is defined in the hello library mentioned in the previous section:
$nm libhello.so

Copyright © Windows knowledge All Rights Reserved