Linux dynamic library usage summary

Source: Internet
Author: User
Linux dynamic library usage summary 1. the basic concept of static library and dynamic library static library is that it has been added to the execution code when the executable program is connected, physically becoming part of the execution program; when you use a static library to compile a program, you do not need to support the library file. it can be used anywhere, but the generated file... information & Linux dynamic library usage summary 1. the basic concept of static library and dynamic library static library is that it has been added to the execution code when the executable program is connected, physically becoming part of the execution program; when you use a static library to compile a program, you do not need to support the library file. it can be used anywhere, but the generated executable file is large. The dynamic library is loaded into the execution program when the executable program starts and can be shared by multiple executable programs. The programs generated by using dynamic library compilation are relatively small, but library files are required for running. if these library files are not available on the machine, they cannot be run. 2. how to use the dynamic library and how the program uses the shared library during connection must be able to find the location of the shared library during running. By default, the executable programs in linux search for the/lib and/usr/lib directories first, and then follow/etc/ld. so. the absolute path of the configuration search in conf. In addition, Linux provides the environment variable LD_LIBRARY_PATH for users to choose from. you can set it to find other paths except the default path, such as/work/lib path, you can go to/etc/rc. d/rc. add the following statement to the script that can be executed after local or other systems are started: LD_LIBRARY_PATH =/work/lib: $ (LD_LIBRARY_PATH ). In addition, the LD_LIBRARY_PATH path takes precedence over the system default path. However, LD_LIBRARY_PATH is set globally. excessive use of LD_LIBRARY_PATH may affect the running of other applications. Therefore, LD_LIBRARY_PATH is mostly used for debugging. We recommend that you use the-R or-rpath option of gcc to specify the library search path during compilation, and save the library path information in the executable file, when running, it will directly go to the path to find the library, avoiding the use of LD_LIBRARY_PATH environment variable search. 3. library Link time path and runtime path modern connector separates Link-time path and Run-time path when processing a dynamic library, you can use-L to specify the path of the database to be connected, and use-R (or-rpath) to specify the path of the database to run the program. This greatly improves the flexibility of the database application. For example, when we perform embedded transplantation # arm-linux-gcc $ (CFLAGS) -o target-L/work/lib/zlib/-llibz-1.2.3 (work/lib/zlib is a cross-compiled zlib library ), after compiling target, we only need to copy the zlib library to the default system path of the Development Board. You can also use-rpath (or-R) and LD_LIBRARY_PATH to specify the search path. Small problem: 1. does the-L option during compilation affect the value of LD_LIBRARY_PATH? For example, the current folder structure is as follows: test. c tools/tool has tool. c tool. h my_err.h and the generated libtool. compile and generate the library file gcc-Wall-g-shared-o tool under sotool. so tool. c references in the current folder: gcc-Wall-g-o test. c-Ltools-ltool compilation does not report an error, but the cannot open shared object file appears during running and loading. If you copy the library file to/usr/lib, there is no error and the database runs normally. It indicates that the-L option during compilation does not affect the environment variable LD_LIBRARY_PATH.-L only specifies the path of the library during program compilation and connection, and does not affect the path of the library during program execution, the system will still find the library required by the program in the default path.
Related Article

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.