Detailed analysis of Linux dynamic library usage

Source: Internet
Author: User

The previous article introduced the characteristics of the Linux dynamic library, such as resource sharing between processes, which makes upgrading some programs simple, you can even sit down to the link and load it completely controlled by the programmer in the program code, and how to create a Linux dynamic library.

Use of Dynamic Link Library

When using a dynamic link library, you first need to let the compiler check some syntax and definitions during compilation.
This is basically the same as the practical use of static libraries. The-Lpath and-lxxx labels are used. For example:

Gcc file1.o file2.o-Lpath-lxxx-o program.exe

The compiler first searches for the libxxx. so file in the path folder. If no file is found, it searches for the libxxx. a static library ).
During the program running, you also need to tell the system where to find your dynamic link library file. In UNIX, the environment variable LD_LIBRARY_PATH is defined. You only need to assign the path value to this variable. Csh command:

Setenv LD_LIBRARY_PATH your/full/path/to/dll

After everything is properly arranged, you can run the ldd command to check whether the connection is normal.

Ldd program.exe

Dynamic Link Library *. so compilation and use --


Dynamic library *. so is often encountered in c and c ++ programming in linux. Recently, I have found several articles on the website to introduce the compilation and link of dynamic libraries, after finally understanding this, I have never been quite familiar with it. Here, I will take a note and provide some help to other brothers who are worried about the dynamic Library Link Library.

◆ 1. Dynamic library Compilation

The following example shows how to generate a dynamic library. Here is a head file: so_test.h, three. c files: test_a.c, test_ B .c, and test_c.c. We compile these files into a dynamic library: libtest. so.

 
 
  1. so_test.h:  
  2.  
  3. #include   
  4. #include   
  5.  
  6. void test_a();  
  7. void test_b();  
  8. void test_c();  
  9.  
  10.  
  11. test_a.c:  
  12.  
  13. #include "so_test.h"  
  14. void test_a()  
  15. {  
  16. printf("this is in test_a...\n");  
  17. }  
  18.  
  19.  
  20. test_b.c:  
  21. #include "so_test.h"  
  22. void test_b()  
  23. {  
  24. printf("this is in test_b...\n");  
  25. }  
  26.  
  27. test_a.c:  
  28.  
  29. #include "so_test.h"  
  30. void test_c()  
  31. {  
  32. printf("this is in test_c...\n");  

Compile these files into a dynamic library: libtest. so
$ Gcc test_a.c test_ B .c test_c.c-fPIC-shared-o libtest. so

◆ 2. Dynamic library links

In 1, we have successfully generated a dynamic link library libtest. so. Next we use a program to call the functions in this library. The source file of the program is test. c.

 
 
  1. test.c:  
  2.  
  3. #include "so_test.h"  
  4. int main()  
  5. {  
  6. test_a();  
  7. test_b();  
  8. test_c();  
  9. return 0;  
  10.  

LINK test. c to the dynamic library libtest. so to generate the execution file test:

$ Gcc test. c-L.-ltest-o test

Test whether to dynamically connect. If libtest. so is listed, the connection is normal.

$ LD_LIBRARY_PATH =. ldd test

Run LD_LIBRARY_PATH =. test to see how it calls functions in the dynamic library.

◆ 3. Compile Parameter Parsing

The most important option is the GCC command line:
-Shared this option specifies to generate a dynamic Connection Library for the connector to generate a T-type export symbol table, sometimes also generate a weak connection W type export symbol), without this flag external programs cannot be connected. Equivalent to an executable file

-FPIC: indicates that the compiled code is in a separate position. If this option is not used, the compiled code is location-related. Therefore, during dynamic loading, the code is copied to meet the needs of different processes, but cannot achieve the purpose of truly sharing code segments.

-L.: indicates that the database to be connected is in the current directory.

-Ltest: an implicit naming rule is used by the compiler to search for a dynamically connected database. That is, add lib before the given name and. so to determine the library name.

LD_LIBRARY_PATH: The environment variable indicates the path where the dynamic connector can load the dynamic library.

If you have the root permission, you can modify/etc/ld. so. conf file, and then call/sbin/ldconfig to achieve the same purpose. However, if you do not have the root permission, you can only use the LD_LIBRARY_PATH output method.

◆ 4. Note

When calling a dynamic library, there are several problems that may occur frequently. Sometimes, the directory where the library header files are already included through "-I, the file where the library is located is guided by the "-L" parameter and the database name of "-l" is specified. However, when you run the ldd command to view the file, you cannot find the so file with the specified link, in this case, you need to modify LD_LIBRARY_PATH or/etc/ld. so. conf file to specify the directory of the dynamic library. This usually solves the problem that the database cannot be linked.

Here is just the use of the Linux dynamic library. It should be well understood in light of the basic knowledge of the previous Linux dynamic library.

  1. Comprehensive Analysis of Linux dynamic library features and Creation
  2. The top 10 Linux versions in China are described in detail)
  3. Linux Regular Expression 1)
  4. Describes how to install a Linux virtual machine.
  5. Details on coexistence of three Linux and windows systems

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.