Locating and searching paths for static and shared libraries

Source: Internet
Author: User
Article Title: static library and shared library location search path. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.

Library files are used when they are connected (static databases and shared libraries) and run (only programs that use shared libraries). Their search paths are set in the system. In Linux, the/lib and/usr/lib directories are used as the default library search paths. Therefore, you can directly use these directories without setting the search path. For libraries outside the default library search path, you need to add the location of the database to the library search path. You can use either of the following methods to set the search path for a library file:

Specify the library search path in the environment variable LD_LIBRARY_PATH.

Add the library search path to the/etc/ld. so. conf file.

It is wise to add the paths that may store library files to/etc/ld. so. conf.

The addition method is extremely simple. It is okay to directly write the absolute path of the library file into it, one line at a time. For example:

/Usr/X11R6/lib

/Usr/local/lib

/Opt/lib

Note that the second search path setting method is sufficient for locating the Library (including the shared library and static library) when the program is connected, however, the execution of programs using the shared library is not enough. This is because in order to speed up the positioning of the shared library during program execution and avoid the inefficiency of searching for the shared library using the search path, it is to directly read the/etc/ld LIBRARY list file. so. from the cache. /Etc/ld. so. cache is a non-text data file and cannot be edited directly. It is based on/etc/ld. so. the search path set in conf is generated by the/sbin/ldconfig command to centralize the shared library files in these search paths (the ldconfig command must be executed with the root permission ).

Therefore, in order to ensure the library location during program execution, in/etc/ld. so. after setting the library search path in conf, you must also run the/sbin/ldconfig command to update/etc/ld. so. cache file. Ldconfig, to put it simply, caches the library files in the paths listed in/etc/ld. so. conf to/etc/ld. so. cache for use. Therefore, after installing some library files (such as just installed glib), or modifying ld. so. after adding a new library path to the conf file, run/sbin/ldconfig to cache all the library files to the ld. so. cache, if not done, even if the library file is clearly under/usr/lib, it will not be used. The result is wrong during compilation, and the xxx library is missing, check and find that the computer is clearly there, and you want to scold the computer.

When a program is connected, you can specify the search path of the library file (static library and shared library) In addition to the preceding settings. You can also use the-L parameter to explicitly specify the search path. Because the path set with-L is preferentially searched, the path of the database to be connected is usually specified in this way.

As mentioned above, there are two ways to set the library search path:

In the environment variable LD_LIBRARY_PATH, Set

In the/etc/ld. so. conf file.

The second setting method requires the root permission to change the/etc/ld. so. conf file and execute the/sbin/ldconfig command. In addition, after the system is restarted, all GTK2-based programs will use the newly installed GTK + Library at runtime. Unfortunately, due to changes in the GTK + version, this may cause compatibility issues to applications and cause some program running to be abnormal. To avoid the above situations, the first method is used to set the search path of the library during installation of GTK + and its dependent libraries. This setting method does not require the root permission and is easy to set:

$ Export LD_LIBRARY_PATH =/opt/gtk/lib: $ LD_LIBRARY_PATH

Run the following command to view the setting content of LD_LIBRAY_PATH:

$ Echo $ LD_LIBRARY_PATH

Now, the two settings of the database are complete.

How to configure the search path for the connected database during cross-Compilation

During cross-compilation, databases on local machines (i686 machines, PC machines, R & D machines) cannot be used. However, the local library is used by default when compiling links, that is, the/usr/lib and/lib directories. Therefore, some methods should be taken during cross-compilation so that the required library can be found during the compilation link.

First, you must know that only the header document is required during compilation, and the actual library document is used for linking. (This is my understanding. If something is wrong, please kindly advise on it.) then, let's talk about how to find the required library during the cross-compilation link.

(1) During cross-compilation, use the-L and-I parameters to specify the path for searching non-standard library documents and header documents. For example:

Arm-linux-gcc test. c-L/usr/local/arm/2.95.3/arm-linux/lib-I/usr/local/arm/2.95.3/arm-linux/include

(2) Use the ld. so. conf document to add the directory where the library is located to this document, and then use the ldconfig command to refresh the cache.

(3) run the following command:

$ Export LD_LIBRARY_PATH = $ LD_LIBRARY_PATH:/usr/local/arm/2.95.3/arm-linux-lib

See the ld. so. conf document and PKG_CONFIG_PATH variable.

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.

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. (For LD_LIBRARY_PATH defects and usage principles, see Why LD_LIBRARY_PATH is bad.) 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.

[1] [2] Next page

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.