Use and build library

xiaoxiao2021-03-05  31

The basic concept of use and generation library has two kinds of dynamics and static. Dynamic is usually used. SO is a suffix, static .a is a suffix. For example: libhello.so libhello.a In order to use different versions of libraries in the same system, you can add the version number after the library file name, for example: libhello.so.1.0, because the program connection defaults. SO is the file suffix name. So in order to use these libraries, it is usually used to create a symbolic connection. ln -s libhello.so.1.0 libhello.so.1 ln -s libhello.so.1 libhello.so When using a library to use a static library, the connector finds the function you need to find, and copy them To the execution file, since this copy is complete, once the connection is successful, the static library will no longer need it. However, for the dynamic library, it is not the case. The dynamic library will leave a tag in the execution program 'specifying that this library must first be loaded when the program is executed. Due to dynamic library saving space, the default operation of Linux is first connected to the dynamic library, that is, if there is a static and dynamic library, it is not particularly specified, will be connected to the dynamic library. Now suppose there is a program development package called Hello, it provides a static library libhello.a, a dynamic library libhello.so, a header file hello.h, providing SayHello () this function / * hello.h * / void Sayhello (); there are some documentation. This typical program development package structure 1. Connect to dynamic library Linux default is to connect with dynamic library, the following program Testlib.c uses the SayHello () function in the Hello library /*testlib.c*/ #include #include int main () {Sayhello (); return 0;} Compile $ gcc -c testlib.c -o testlib.O with the following command: $ GCC Testlib.o -lhello -o testlib At connection time, it is necessary to assume that libhello.o and libhello.a are under the default library search path / usr / lib, if you want to add -L parameters in other locations, troubles with static library Some, mainly the parameters. Or the above example: $ gcc testlib.o -o testlib -wi, -bstatic -lhello Note: This special "-wi, -bstatic" parameter is actually transmitted to the connector LD. Indicate it to connect to a static library If there is only a static library in the system, it is certainly no need for this parameter. If you want to connect with multiple libraries, and each library is different, for example, the above programs are quantitatively connected to libhello, and the Libbye is dynamically connected, and their commands should be: $ GCC Testlib.o - o Testlib -wi, -bstatic -lhello -wi, -bdynamic -lbye 3. Dynamic library path problem In order to make the executive to find a dynamic library, there are three ways: (1) copy the library to / usr / lib and / lib and / lib Under contents. (2) Plus the path where the library is located in the LD_Library_Path environment variable. For example, dynamic library libhello.so In / Home / Ting / Lib Directory, use Bash as an example, use command: $ export ld_library_path = $ ld_library_path: / home / ting / lib (3) modify /etc/ld.so.conf file , Add the path where the library is added to the end of the file and performs LDConfig refresh.

转载请注明原文地址:https://www.9cbs.com/read-33639.html

New Post(0)