The second person's understanding about the priority of the Linux dynamic library search path

Source: Internet
Author: User
The second person's understanding of the Linux dynamic library search path priority problem-general Linux technology-Linux programming and kernel information. The following is a detailed description. Author: annight E-mail: annightzhao@gmail.com

May 5
Search priority for dynamic libraries in LINUX

When running GCC, the program will first generate the. o file through the compiler. This file cannot be run directly. The executable file must be generated again before it can be run by the system. If the program involves calling the library function, the static library (. o) and call the dynamic library (. so). Generally, static databases can be called directly, which is convenient to call. However, all functions that need to call library files must call a library function, this makes the program occupy a large amount of memory space (if the program has many functions that need to call library functions), and the dynamic library needs to specify the path (the system will search for library functions when running the program ), however, all functions that need to call the database share one library function.

The default search paths for Linux dynamic libraries are/lib and/usr/lib. After a dynamic library is created, it is generally copied to these two directories. When a dynamic library is required for program execution and the dynamic library is not loaded into the memory, the system will automatically go to the two default search paths to find the corresponding dynamic library file, then load the file to the memory so that the program can use the functions in the dynamic library and other resources of the dynamic library.

Today, I learned about the dynamic library by specifying several paths (several search methods) and their priorities. The specific method is to specify different paths for four different file libraries, which are called by one program together, because the Search priority level will call different library functions.

LINUX dynamic libraries generally have four search methods (high by priority ---> LOW ):

Create several files first:
1. Interface: tlib. h
# Ifndef _ tlib_h
# Define _ tlib_h

Void test (void );

# Endif

2. tlib. c
# Include "tlib. h"
# Include

Void test ()
{
Printf ("This is test (1, 2, 3, 4) \ n ");

}

3. Execution program: test. c
# Include
# Include "tlib. h"

Int main ()
{
Test ();
Return 0;
}

1. Use the environment variable LD_LIBRARY_PATH to specify the dynamic library search path.
You can specify the dynamic library search path by setting the environment variable LD_LIBRARY_PATH. When multiple dynamic library search paths are specified using this environment variable, the paths are separated by colons. The specific method is as follows:
Gcc? FPIC? Wall? G? C tlib. c
Gcc? Shared? FPIC? O libtlib. so tlib. o (note that the generated dynamic library function must start with lib)
Gcc? O test. c? L .? Ltlib (the L parameter refers to the Dynamic Inventory location. It refers to the current path; the l parameter refers to the name of the dynamic library, where tlib removes the front lib of libtlib)
Export LD_LIBRARY_PATH = /~ (~ Refers to the path where you store. H files)

2. Specify the dynamic library search path in the configuration file/etc/ld. so. conf.
Gcc? FPIC? Wall? G? C tlib. c
Gcc? Shared? FPIC? O libtlib. so tlib. o
Gcc? O test test2.c? L .? Ltlib

Open the/etc/ld. so. conf file and add the path of your. h file. Ldconfig (saves and imports the modified configuration file)

3. Add the dynamic library to/lib.
Gcc? FPIC? Wall? G? C tlib. c
Gcc? Shared? FPIC? O libtlib. so tlib. o
Gcc? O test test3.c? L .? Ltlib
Mv libtlib. so/lib

4. Add the dynamic library to/usr/lib.
Gcc? FPIC? Wall? G? C tlib. c
Gcc? Shared? FPIC? O libtlib. so tlib. o
Gcc? O test test4.c? L .? Ltlib

Mv libtlib. so/usr/lib

Finally, run the test file to get test1. In order to test, the second step will cancel the global variable, and then run the test file to get test2, and so on.

Some ideas may be wrong. Please give me more advice.

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.