Find paths for Linux dynamic libraries

Source: Internet
Author: User

The first two days to write a dynamic library, and then try to compile into the program to run, the results found that the compile time through the GCC-l parameter to specify the path can only let the compiler pass, the runtime will still be problematic.

For example, the following:

MAIN.C is the main program, the middle of the SUM.C contains a function add, which is used to perform the addition, the code is as follows:

1 /*2 * MAIN.C3  */4#include <stdio.h>5 6 intAddintAintb);7 8 intMainintargcChar*argv[])9 {Tenprintf"sum =%d\n", Add (3,5)); One     return 0; A}

1 /* 2 * SUM.C 3  */ 4 int Add (intint  b)5{6      return A + b; 7 }

The result of the error is as follows:

I at compile time through-l specified to find the location of the dynamic library, the result when the run still can not find my own writing of the libsum.so this dynamic library, and then to check a bit, just understand the story.

The program is divided into 2 steps when linking dynamic libraries, compile-time links and run-time links.

1. Compile-time links

This process is performed by the LD program, so you will often see this error when the location of the dynamic library is not found at compile time:

  

This process is strictly not a link, because in this case, the LD program does not actually write the code of the function inside the library into the executable file, but some of the symbols and other necessary information are written in the executable file, for the executable file to run the search.

In general, LD has done two things in this step:

1. Find out if the dynamic library contains the symbols (functions and global variables) that we need, and if they are found, the link allows you to generate the executable file.

2. Symbols and other necessary information (such as the address of the symbol) are written in the executable file for the executable file to look for when it runs.

2. Run-time Links

This process is performed by the ld-linux.so program, which is the real link. What it does is map the code of the dynamic library to the process (the executable file is the process ...). ) in the virtual address space for the process to invoke.

For more information on linking, loading, and running, refer to < reference article 1> (see the detailed address at the end of this article).

OK, understand the above two links, we look at the two links to find the directory location of the dynamic library, as follows:

At run time, ld-linux.so find the order of shared libraries

(1) ld-linux.so.6 is specified in the executable target file and can be viewed with the readelf command
(2) Ld-linux.so.6 default in/usr/lib and Lib search; when glibc is installed under/usr/local, it looks for/usr/local/lib
(3) The path set in the LD_LIBRARY_PATH environment variable
(4) The path specified in/etc/ld.so.conf (or/usr/local/etc/ld.so.conf), generated by ldconfig in binary Ld.so.cache

At compile time, ld-linux.so find the order of shared libraries

(1) ld-linux.so.6 is set by the GCC spec file
(2) The path printed out by GCC--print-search-dirs is mainly libgcc_s.so and other libraries. Can be set by Gcc_exec_prefix.
(3) The path set in the LIBRARY_PATH environment variable, or the-l/usr/local/lib specified in the compiled command line
(4) The default search path order set by LD in Binutils, specified when compiling binutils . (available through the LD--verbose | grep search "to view)
(5) The search path order of the binary program is set in the PATH environment variable. General/usr/local/bin higher than/usr/bin
(6) The search path order of the header files at compile time is similar to the library's lookup order. General/usr/local/include higher than/usr/include

Note that the path that is found at compile time can be specified by the gcc-l parameter or Library_path, but the run-time lookup path does not contain the path specified by the GCC-L and Library_path environment variables, so that is the problem that we started with. Compile time through-l to specify the dynamic library search path, compile also passed, but the runtime will be error, this is because the runtime to find the path of the dynamic library is not specified, so we write the dynamic library can not be found, and to solve this problem, by setting the environment variable Ld_library_path or modify/ Etc/ld.so.conf (Remember to change the run Ldconfi to generate Ld.so.cache). As shown in the following:

Reference article:

1. C Compiler, linker, loader detailed

2. Ld-linux.so the order in which to find shared libraries

3. Readelf command

Find paths for Linux dynamic libraries

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.