Location search path for static and shared libraries (during compilation and runtime)

Source: Internet
Author: User
Library files are connected (static library and shared
Library) and run (only for programs that use shared libraries). The search path is set in the system. Average
In Linux,/lib and/usr/lib
The two directories are used as the default library search path. Therefore, you can directly use these directories without setting the search path. For libraries outside the default library search path, you must add the location of the database
In the search path of the database. 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
In order to speed up the positioning of shared libraries during program execution and avoid the inefficiency of searching for shared libraries using search paths, the shared library list file is read directly.
/Etc/lD. So. cache. /Etc/lD. So. Cache
Is a non-text data file and cannot be edited directly. It is based on/etc/lD. So. conf
In/sbin/ldconfig
Command to combine the shared library files in these search paths.
Permission execution ).
Therefore, in order to ensure the library location during program execution
After setting the library search path, you must also run the/sbin/ldconfig command to update
/Etc/lD. So. cache file. Ldconfig
To put it simply, it 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 (for example, just installed Glib), or modifying lD. So. conf to add a new library path, run
/Sbin/ldconfig caches all library files to lD. So. cache. If it is not done, it will not be used even if the library files are clearly under/usr/lib.
In the compilation process, the result is wrong. If the xxx library is missing, you can check it and find that it is clearly there.
When a program is connected, the search path of the library file (static library and shared library) can be used in addition to the above settings
-L parameters are explicitly specified. Because-L is used
The configured path will be searched first, so 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
/Sbin/ldconfig command. In addition, when the system restarts, all
The newly installed GTK + Library will be used when the program is running. Unfortunately, due to GTK +
Version changes, which may cause compatibility issues to applications and cause program running to be abnormal. To avoid the above situations
GTK +
The first method is used to set the search path of the library during the installation of the dependent library. This setting method is not required
Root permission, 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 when cross-compiling links.
(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. (LD_LIBRARY_PATH
For more information, 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, at runtime, it will go directly to this path
Path Search Library to avoid using the LD_LIBRARY_PATH environment variable search.
(4) use the configure parameter of the software during cross-compilation. For example, when I compile a minigui-1.3.3, use the following Configuration:
#! /Bin/bash
Rm-F config. cache config. Status
./Configure -- Build = i686-linux -- Host = arm-Linux -- target = arm-Linux \
Cflags =-I/usr/local/ARM/2.95.3/ARM-Linux/include \
Ldflags =-L/usr/local/ARM/2.95.3/ARM-Linux/Lib \
-- Prefix =/usr/local/ARM/2.95.3/ARM-Linux \
-- Enable-lite \
-- Disable-galqvfb \
-- Disable-qvfbial \
-- Disable-vbfsupport \
-- Disable-ttfsupport \
Disable-type1support \
Disable-imegb2312py \
-- Enable-extfullgif \
-- Enable-extskin \
-- Disable-videoqvfb \
-- Disable-videoecoslcd
Here I have configured the cflags and ldflags parameters, so that I don't have to modify the-L and-I parameters in each makefile, nor need to configure LD_LIBRARY_PATH or rewrite lD. so. CONF file.
Linux dynamic library usage Summary
1. Basic concepts of static and dynamic libraries
The static library is added to the Execution Code when the executable program is connected and physically becomes a part of the execution program. The library file is not supported when the program is compiled using the static library, it can be used everywhere,
Large executable files. The dynamic library is loaded into the execution program when the executable program starts and can be shared by multiple executable programs. The program generated by using dynamic library compilation is relatively small, but the library is required for running
File support. If these library files are not in the machine, they cannot be run.
2. How to use dynamic libraries
If a shared library is used by a program during connection, you must find the location of the shared library during running. In Linux, the executable program searches for/lib and/usr/lib by default.
And then search for the absolute path according to the configuration in/etc/lD. So. conf. Linux also provides the environment variable LD_LIBRARY_PATH for users to choose from.
You can set it to find other paths except the default path, such as the/work/lib path. You can go to/etc/rc. d/RC. local or other system startup
Add the following statement to the script: LD_LIBRARY_PATH
=/Work/lib: $ (LD_LIBRARY_PATH ). In addition, the LD_LIBRARY_PATH path takes precedence over the default path of the system.
LD_LIBRARY_PATH).
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. (LD_LIBRARY_PATH
For more information, 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, at runtime, it will go directly to this path
Path Search Library to avoid using the LD_LIBRARY_PATH environment variable search.
3. Path and runtime path of the database connection
Link-time
Path) and the runtime path (run-time
Path). You can use-L to specify the path of the database to be connected, and use-R (or-rpath) to specify the path of the library to run the program, which greatly improves the flexibility of the library application. For example, we do embedded
Porting # arm-Linux-GCC $ (cflags)-O target-L/work/lib/zlib/-llibz-1.2.3
(Work/lib/zlib is a cross-compiled zlib library.) After compiling the target, we only need to copy the zlib library to the default system path of the Development Board. Or use-
Rpath (or-R) and LD_LIBRARY_PATH specify the search path.
Minor issues:
1. Does the-L option during compilation affect the value of LD_LIBRARY_PATH?
For example:
The current folder structure is as follows:
Test. c tools/
Tool. c tool. h my_err.h and the generated libtool. So
Compile and generate library files under Tool
Gcc-wall-g-shared-O tool. So tool. c
Reference in the current folder:
Gcc-wall-g-o test. C-ltools-ltool
No error is reported during compilation, but the cannot open shared object file is displayed during running and loading.
If you copy the library file to/usr/lib, there is no error and the database runs normally.
It indicates that the-L option during compilation does not affect the environment variable LD_LIBRARY_PATH.-l only specifies the path of the library during program compilation and connection, and does not affect the path of the library during program execution, the system will still find the library required by the program in the default path.
Precautions for Dynamic Link Library
1. Precautions for dynamic link libraries.
If the program with GTK contains <GTK \ GTK. h>
Then you need to add the parameters of the dynamic link library.
$ GCC test_a.c test_ B .c iptbox. C-FPIC-shared-O iptbox. So 'pkg-config
-- Cflags -- libs GTK ++-2.0
The generated. So Dynamic Link Library must be stored in the usr/lib directory.
CP libipt. So/usr/lib
2. If an error occurs while running the AP, you can use the following methods to find the problem:
Strace./test
3. Find the path in the folder:
Find/usr/lib-name libept. So
4. Decompression command:
Tar-zxvf file name
5. RPM package solution:
# Rpm-IVH scim-1.4.7.4-1benX.src.rpm
# Cd/usr/src/Dorado/specs
# Rpmbuild-BB scim. spec
6. Open Directory Permissions:
Chmod 777 root (directory name)
7. Install software in Linux:
A ../configure
B. Make
C. make install
8. Update Library Reference:
Ldconfig
9. Check whether the AP is installed.
# Whereis scim-ppenglish

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.