Generally, if the header file of the library is not in the/usr/include directory, you must use the-I parameter to specify the path during compilation. Because the same library may be located in different directories on different systems, users can install the library in different directories during library installation, even if the same library is used, because of the different library paths, the path of the header file specified by the-I parameter may also be different. The result is that the compilation command interface is inconsistent. If the-l parameter is used, the connection interface may be inconsistent. Inconsistent compilation and connection interfaces may cause trouble for library usage.
Some solutions have been found to solve the problem of inconsistent compilation and connection interfaces. The basic idea is to store the location information of the database in advance, and extract useful information from it through a specific tool for compilation and connection. In this way, the compilation and connection interface are consistent. Among them, the most common Library Information Extraction Tool is PKG-config described below.
PKG-config is A. PC file provided by the database to obtain various necessary information about the database, including the version information, parameters required for compilation and connection. This information can be extracted separately using the parameters provided by PKG-config for the compiler and connector.
Thepkgconfig package contains tools for passing the include path and/or librarypaths to build tools during the make File Execution.
PKG-configis a function that returns meta information for the specified library.
Thedefault setting for pkg_config_path is/usr/lib/pkgconfig because of the prefixwe use to install pkgconfig. You may add to pkg_config_path by exportingadditional paths on your system where pkgconfig files are installed.
Is only needed when compiling packages, not during Run-Time.
By default, the. PC file corresponding to each library that supports PKG-config is located in the LIB/pkgconfig directory in the installation directory after installation. For example, if you have installed glib in the/opt/GTK directory. PC file is/opt/GTK/lib/pkgconfig directory named glib-2.0.pc file:
Prefix =/opt/GTK/
Exec_prefix =$ {prefix}
Libdir =$ {exec_prefix}/lib
Includedir =$ {prefix}/include
Glib_gen1_al = glib-gen1_al
Gobject_query = gobject-Query
Glib_mkenums = glib-mkenums
Name: glib
Description: C utility Library
Version: 2.12.13
Libs:-L ${libdir}-lglib-2.0
Cflags:-I $ {includedir}/glib-2.0-I $ {libdir}/glib-2.0/include
The -- cflags parameter of PKG-config can be used to provide the required options during compilation, while the -- libs parameter can provide the options during connection. For example, if a sample. C program uses the glib library, you can compile it as follows:
$ Gcc-C 'pkg-config -- cflags glib-2.0 'sample. c
Then connect as follows:
$ GCC sample. O-o Sample 'pkg-config -- libs glib-100'
The preceding two steps can also be merged into the following steps:
$ GCC sample. C-o Sample 'pkg-config -- cflags -- libs glib-100'
As you can see, because the PKG-config tool is used to obtain the library options, the same compilation and connection commands can be used no matter what directory the library is installed in, the compilation and connection interfaces are unified.
There are two basic prerequisites for extracting library compilation and connection parameters using the PKG-config tool:
1. The Library itself must provide a corresponding. PC file during installation. The library description does not support the use of the PKG-config tool.
2. PKG-config must know where to find the. PC file.
GTK + and its dependent libraries support the PKG-config tool, so the remaining problem is how to tell PKG-config where to find the corresponding library. PC file, which is also solved by setting the search path.
For GTK + and its dependent libraries that support the PKG-config tool, the search path of the header file of the library is changed to the search path of the. PC file .. The search path of the PC file is set through the Environment Variable pkg_config_path. PKG-config searches the file in the order of the set path until the specified. PC file is found.
After installing glib, configure the following settings in Bash:
$ Export pkg_config_path =/opt/GTK/lib/pkgconfig: $ pkg_config_path
Run the following command to check whether the/opt/GTK/lib/pkgconfig path has been set in the pkg_config_path environment variable:
$ Echo $ pkg_config_path
After this setting, when using other programs or libraries of the glib library, PKG-config knows to first find the glib-2.0.pc in the/opt/GTK/lib/pkgconfig directory (GTK + and other. PC files will also be copied here, and the corresponding files will be searched here first. PC files ). Then, you can use PKG-config to extract the library compilation and connection parameters for the program to compile and connect.
Note that the environment variable setting is only valid for the current terminal window. If there is a terminal window without the above settings, PKG-config will not find the newly installed glib-2.0.pc file, which may cause subsequent installation (such as installation of ATK after Glib) unable to proceed.
In our installation scheme, environment variables are used to set GTK + and its dependent libraries. Therefore, when the system restarts or a new terminal window is opened, to use the newly installed GTK + library, you need to reset the pkg_config_path and LD_LIBRARY_PATH environment variables as above.
Before using GTK +, you need to set the library. Although it seems a little complicated, it is the safest way to use the GTK + Library, and does not use the GTK + library programs (such as GNOME Desktop) that already exist on the system) brings any impact.
To make the library settings easier, you can save the following two sentence settings to a file (such as a set_gtk-2.10 file ):
Export pkg_config_path =/opt/GTK/lib/pkgconfig: $ pkg_config_path
Exportld_library_path =/opt/GTK/lib: $ LD_LIBRARY_PATH
Then, you can use the following method to set the database (the source command can also be replaced ):
$ Source set_gtk-2.10
The above settings are necessary only when you use the new version of GTK + Library to develop applications or run programs that use the new version of GTK + Library.
If you want to avoid the trouble of using the GTK + library before the above settings, you can set the above two environment variables in the system configuration file (such as/etc/profile) or in your own user configuration file (such ~ /. Bash_profile); the library search path can also be set in the/etc/lD. So. conf file, and so on. This setting takes effect when the system starts, which may cause GTK + programs to use the new GTK + Runtime Library, which may cause some problems. Of course, if you find that it is no problem to replace the old version with the new version of GTK +, this setting method is more convenient. Add ~ /. Bashrc, for example:
Pkg_config_path =/opt/GTK/lib/pkgconfig
Remember: The two paths are separated by ':', not ',', or ';'. It will not cause major problems.
After restart:
[Root @ localhost ~] # Echo $ pkg_config_path
/Opt/GTK/lib/pkgconfig
Ii. Runtime
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:
1. Specify the library search path in the environment variable LD_LIBRARY_PATH.
2. Add the library search path to the/etc/lD. So. conf file.
It is wise to add the paths that may store the 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 (ldconfig
Command ). Therefore, to ensure the library location during program execution, after setting the library search path in/etc/lD. So. confYou must run the/sbin/ldconfig command to update the/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.
Pai_^
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 and 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 sometimes causes compatibility issues for applications and causes 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.