Solution to link library not found when connecting to dynamic link library

Source: Internet
Author: User
solution to link library not found when connecting to dynamic link library

When you link to a dynamic library, you may receive an error that cannot find the link library, and if you use the LDD command, you can see all of the dependencies of the executable file. In the "Linux C one-stop programming" book to see some solutions, first summed up the following four methods: Method A

If you are only using it temporarily in development, you can use the ld_library_path variable to set

The path of the library to which EXPORT ld_library_path= is currently dependent

This approach is only temporary and if you re-enable a terminal, you need to reset it once. Method Two

To connect a dynamic-link library is determined by the dynamic linker, you first need to know how the dynamic linker looks for the library. First look in the path recorded by the environment variable ld_library_path , if not found, proceed to the next step from the cached file /etc/ld.so.cache , under root permission, use the Ldconfig command, Read the/etc/ld.so.conf file and generate the cached file if the dynamic library is not found in either of these ways, the dynamic linker looks in the system default path, finds it in/usr/lib/, and then finds it in/lib

Then, according to the above dynamic linker Lookup library method, writes the library path to the/etc/ld.so.conf file, then runs Ldconfig (ldconfig-v can view the detailed information), can generate the cache file, the connector can find the library file method three

Copy the library file directly to the/usr/lib or/lib directory, and let the dynamic linker find method four in the system default path

When compiling with GCC, add option-wl,-rpath, the path to the dynamic library, which means that the-rpath dynamic library path is the option passed by GCC to the linker.

GCC main.c-g-L-lstack-istack-o Main-wl,-rpath, $PWD

GCC compile main.c, link dynamic library libstack.so, header file in the./stack directory, set up the library file path in the current path

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.