Linux dynamic library search path priority

Source: Internet
Author: User
Linux dynamic library search path priority-general Linux technology-Linux programming and kernel information. For details, see the following. By Ronnie: 13761598350 E-mail: ghnghnghnghn@163.com

Generally, the default search paths for Linux dynamic libraries are/lib and/usr/lib. After a dynamic library is created, the created dynamic library is usually copied to these two directories. When running the application, the system will go to these two paths to call the dynamic library. However, in addition to the two methods to specify the search path for a dynamic library, there are two other methods: editing the configuration file and specifying the environment variables. However, these four search paths have different priorities.

We first create three files tlib. h In the directory at the same level:

# Ifndef _ TLIB _
# Define _ TLIB _

Void fun (void );

# Endif

Tlib. c:

# Include
# Include "tlib. h"

Void fun ()
{
Printf ("This is a test \ n ");
}

And test. c:

# Include
# Include "tlib. h"

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

In general, no matter which method is used to specify the search path of the dynamic library, these steps are indispensable:

① Gcc? FPIC? Wall? C tlib. c generate the. o file

② Gcc-shared-fPCI-o libtlib. so tlib. o to generate a dynamic library

③ Gcc-o test. c-L .? Ltlib generates executable files

Note that the dynamic library file name must start with lib.

The following describes the four methods one by one:

To make the test more intuitive, we can change the content printed by the fun function in tlib. c before doing each method, and then perform steps ① To ③ above.

Method 1:
Add the path of the dynamic library to the configuration file/etc/ld. so. conf. After editing, enter ldconfig in the command line.

Method 2:
Specify the environment variable export LD_LIBRARY_PATH = [path of the dynamic library].

Method 3:
Copy the dynamic library to the/usr/lib directory.

Method 4:
Copy the dynamic library to the/lib directory.

After testing, we can see that the priority of the search path in a dynamic library is from high to low: method 2, method 1, Method 4, and method 3.
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.