Go to: Search Path for header files and library files during Linux GCC Compilation

Source: Internet
Author: User

From: http://blog.csdn.net/rlj021/archive/2008/12/16/3530939.aspx

I. the header file GCC looks for the required header file during compilation: ※the search starts from-I. ※then, find the GCC environment variable c_include_path, cplus_include_path, objc_include_path. ※find the specified directory/usr/include.
/Usr/local/include/usr/lib/GCC-lib/i386-linux/2.95.2/include/usr/lib/GCC-lib/i386-linux/2.95.2 /.. /.. /.. /.. /include/g ++-3/usr/lib/GCC-lib/i386-linux/2.95.2 /.. /.. /.. /.. /i386-linux/include library file but if GCC is installed, there is a given prefix, then/usr/include
Prefix/includeprefix/xxx-gnulibc/includeprefix/lib/GCC-lib/XXXX-xxx-gnulibc/2.8.1/include 2. when compiling the library file: ※gcc will go to-l ※find the GCC environment variable LIBRARY_PATH. ※find the preset directory/lib:/usr/local/lib: this was written in the program at the time of compile GCC.

 

Iii. Search Path of dynamic library during runtime

1. Specify the dynamic library search path in the configuration file/etc/lD. So. conf.

2. Specify the dynamic library search path through the Environment Variable LD_LIBRARY_PATH (when multiple dynamic library search paths are specified through this environment variable, the paths are separated by colons) 3. Specify the dynamic library search path of the program when compiling the target code (you can also specify the dynamic library search path of the program when compiling the target code. This is specified through the GCC parameter "-wl,-rpath. When multiple dynamic library search paths are specified, use the colon ": "separated 4. The default dynamic library search path/lib/usr/lib can be obtained by executing the executable file POs to find the dynamic library to be searched, in this way, you can obtain the search sequence of 1st dynamic databases, delete the dynamic library, execute the POs program, obtain the search paths of 2nd dynamic databases, and then delete 2nd searched dynamic libraries, in this way, the sequence of Linux dynamic database search is obtained. The correspondence between output results of program POS execution and the searched dynamic library is shown in table 1. The dynamic library corresponding to the POs output results is used to specify the dynamic library search path. /. /libpos. so the dynamic library search path specified when the target code is compiled/root/test/ENV/lib/libpos. the dynamic library search path specified by the so environment variable LD_LIBRARY_PATH/root/test/CONF/lib/libpos. so configuration file/etc/lD. so. the dynamic library search path specified in CONF/lib/libpos. so default dynamic library search path/lib/usr/lib/libpos. the default search path of the so dynamic library/usr/lib dynamic library is 1. the dynamic library search path specified when the target code is compiled; 2. the dynamic library search path specified by the Environment Variable LD_LIBRARY_PATH;
3. The dynamic library search path specified in the configuration file/etc/lD. So. conf;
4. The default dynamic library search path is/lib/usr/lib.

In Linux, the shared library mechanism is similar to the high-speed cache mechanism, saving the library information in/etc/lD. So. cache.

When the program is connected, first find the file, and then go to the path of LD. So. conf to find details.

This is why ldconfig needs to be run again after lD. So. conf is modified.

Additionally, ldconfig is in/sbin.

Ldconfig
1. add something to/lib and/usr/lib without modifying/etc/lD. so. conf, but after the execution, you need to call ldconfig. Otherwise, this library will not be found.
2. When you want to add something out of the above two directories, you must modify/etc/lD. So. conf and then call ldconfig. Otherwise, you will not be able to find it.
For example, if you have installed a MySQL file to/usr/local/MySQL, MySQL has a lot of Libraries under/usr/local/MySQL/lib. In this case, you need to go to/etc/lD. so. add/usr/local/MySQL/lib at the top of the conf file, which must be the first line and be specified to the directory of the library file, instead of its parent directory, after ldconfig is saved, the new library can be found when the program is running. You can directly use ldconfig/usr/local/MySQL/lib to save all the shared library paths in this directory to lD. so. in the cache, they are not written to lD. so. CONF file
3. If you want to put lib outside these two directories, but you do not want to add anything in/etc/lD. So. conf (or you do not have the permission to add anything ). That is, the global variable LD_LIBRARY_PATH export will be used to find the library in the directory when running the program. Generally, this is only a temporary solution that can be used when you do not have the permission or need it temporarily.
4. ldconfig is related to running the program and has nothing to do with compiling. During compilation, you should add-L instead of obfuscation.
5. In short, it is recommended that you perform ldconfig for any changes to the library. Otherwise, unexpected results may occur. It won't take too much time, but it will save a lot of time.

Ldconfig is usually run when the system starts. When you install a new dynamic link library, you need to manually run this command.
The ldconfig command line is used as follows:
Ldconfig [-v | -- verbose] [-N] [-N] [-x] [-F conf] [-C cache] [-r root] [-L] [-p | -- print-Cache]
[-C format] [-- format = format] [-V] [-? | -- Help | -- usage] path...
The options available in ldconfig are described as follows:
(1)-V or -- verbose: When this option is used, ldconfig displays the directory being scanned, The searched dynamic link library, and the name of the connection it created.
(2)-N: When this option is used, ldconfig only scans the directory specified by the command line and does not scan the default directory (/lib,/usr/lib ), nor scan the configuration file/etc/lD. so. conf directory.
(3)-N: This option indicates that ldconfig does not recreate the cache file (/etc/lD. So. cache). If the-x option is not used, ldconfig updates the file connection as usual.
(4)-X: This option indicates that ldconfig does not update the file connection. If the-n option is not used, the cached file will be updated normally.
(5)-f conf: This option specifies that the configuration file of the dynamic link library is Conf, and the default value is/etc/lD. So. conf.
(6)-C cache: This option specifies that the generated cache file is cache. The default value is/etc/lD. so. cache. This file stores the list of dynamically linked libraries that can be shared in the sorted order.
(7)-R root: This option changes the root directory of the application to root (implemented by calling the chroot function ). when this option is selected, the system default configuration file/etc/lD. so. conf, which corresponds to root/etc/lD. so. conf. for example, when-r/usr/ZZZ is used, open the configuration file/etc/lD. so. in Conf,/usr/ZZZ/etc/LD is actually enabled. so. CONF file. this option can greatly increase the flexibility of dynamic link library management.
(8)-L: Generally, ldconfig automatically establishes a connection to the dynamic link library when searching for a dynamic link library. when this option is selected, the expert mode is enabled. You need to manually set the connection. this option is not required for general users.
(9)-P or -- print-Cache: This option indicates that ldconfig prints the names of all the shared libraries saved in the current cache file.
(10)-C format or -- format = format: This option is used to specify the format used by the cache file. There are three types: LD (old format), new (new format) and compat (compatible format, which is the default format ).
(11)-V: This option prints the version information of ldconfig and then exits.
(12 )-? Or -- help or -- Usage: the three options share the same role, so that ldconfig prints the help information and then exits.

In Linux, the shared library mechanism is similar to the high-speed cache mechanism, saving the library information in/etc/lD. So. cache.
When the program is connected, first find the file, and then go to the path of LD. So. conf to find details.
This is why ldconfig needs to be run again after lD. So. conf is modified.

3. Dynamic library Path Problems
There are three methods for the execution program to smoothly find the dynamic library:
(1) copy the database to the/usr/lib and/lib directories. Then run ldconfig
(2) Add the path of the library in the LD_LIBRARY_PATH environment variable. For example, if the dynamic library libhello. So is in the/home/Ting/lib directory and bash is used as an example, run the following command:
$ Export LD_LIBRARY_PATH = $ LD_LIBRARY_PATH:/home/Ting/lib
(3) modify the/etc/lD. So. conf file, add the path of the Library to the end of the file, and execute ldconfig refresh. In this way, all the library files under the added directory are visible,
Note: This file can be found during the Link (generally through the-l library file directory), does not mean that the file can also be found during running, only the above three methods can ensure that the dynamic library is found during the runtime. (This problem does not exist in the static library because when the static library is in the LD, the file content has been copied to the target file and will not be searched during running)

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.