Forwarded from: http://www.laruence.com/2010/01/27/1265.html#compilation and Connection
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.
- The pkgconfig package contains tools for passing the include path and/or library
- paths to build tools during the make file execution.
-
- pkg-config is a function that returns meta information for the specified library.
-
- The default setting for PKG_CONFIG_PATH is /usr/lib/pkgconfig because of the prefix
- we use to install pkgconfig. You may add to PKG_CONFIG_PATH by exporting additional
- paths on your system where pkgconfig files are installed. Note that PKG_CONFIG_PATH 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_genmarshal=glib-genmarshal
- 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-2.0`
The preceding two steps can also be merged into the following steps:
- $ gcc sample.c -o sample `pkg-config --cflags --libs glib-2.0`
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:
The Library itself must provide a corresponding. PC file during installation. The library description does not support the use of the PKG-config tool.
PKG-config must know where to find this. 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
- export LD_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
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:
- 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 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 (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/ldco. in the cache, if it is not done, it will not be used even if the library file is clearly under/usr/lib, and the result compilation is clearly there, I want to yell at computer pig. Pai_^
When a program is connected, the search path for the library file (static library and shared library) is exclusive to the preceding setting method. Because the path set with-l will be searched first, it is usually specified directly during connection.
As mentioned above, there are two ways to set the library search path: In the environment variable ld_library_pa file. The second setting method requires the root permission to change/etc/lD. so. after the conf file is restarted, all gtk2-based programs will use the newly installed GTK + Library at runtime. Unfortunately, it may cause compatibility issues to applications, resulting in Abnormal Running of some programs. In order to avoid the above situations, the first method is used to set the search path of the database. This setting method does not require the root permission.
- $ 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.