Brief introduction of the relationship between configure, Pkg-config and Pkg_config_path
First, what is configure
Source installation process Most will use configure this program, the general configure is a script, execution can pass in the necessary parameters to inform the configuration items.
Configure program it will be based on the incoming configuration items to check the environment on which the program compiles and to configure the program compilation installation, and ultimately generate the required makefile files for the program make read-in and use to call the relevant compiler (usually called the compiler is GCC) To compile the final binary program. While the Configure script examines the appropriate dependent environment (e.g., the version of the software that is being relied upon, the corresponding library version, and so on), the Pkg-config tool is often used to detect the dependent environment.
Second, what is Pkg-config
1, Pkg-config Introduction
Pkg-config is used to retrieve information about the installation library files in the system. Typically, it is used as a library for compiling and connecting. As in Makefile:
PROGRAM:PROGRAM.CCC program.c ' pkg-config--cflags--libs gnomeui '
2. Pkg-config function
In general, if the header file of the library is not in the/usr/include directory, you need to specify its path with the-I parameter at compile time. Because the same library may be located in different directories on different systems, users can install the library in different directories, so even if the same library, due to the different path of the library, the path of the header file specified with the-I parameter, and the path to specify the Lib library with the-l parameter when connecting, may be different. The result is a non-uniform compiler command interface. This may cause problems when the same program is copied from one machine to another, due to inconsistencies in compilation and connectivity.
Pkg-config is a tool used to solve the problem of non-uniformity of the compiler interface.
Its basic idea: Pkg-config is a. pc file that is provided through the library to obtain all the necessary information about the library, including version information, compilation, and connection parameters required. When needed, the required information can be extracted for compilation and connection using the parameters provided by Pkg-config (–cflags,–libs). In this way, regardless of where the library files are installed, the. pc files corresponding to the library can be accurately located, using the same compilation and connection commands, making the compilation and connection interface uniform.
The main functions it provides are:
<1> Check the version number of the library. If the desired version of the library does not meet the requirements, print out the error message and avoid connecting to the wrong version of the library file.
<2> gets the path to the header file for the compilation preprocessing parameters, such as the macro definition.
<3> get compile parameters, such as the location of libraries and other libraries they depend on, filenames, and other connection parameters.
<4> automatically joins the settings of other libraries that depend on it.
3, glib-2.0. pc File Content Example
By default, the. pc files for each library that supports Pkg-config are located in the Lib/pkgconfig directory in the installation directory after installation. For example, we have installed glib in the/OPT/GTK directory above, then the glib library corresponding to the. pc file is the/opt/gtk/lib/pkgconfig directory under a file called glib-2.0.pc:
Prefix=/opt/gtk/exec_prefix=${prefix}libdir=${exec_prefix}/libincludedir=${prefix}/includeglib_genmarshal= Glib-genmarshalgobject_query=gobject-queryglib_mkenums=glib-mkenumsname:glibdescription:c Utility LibraryVersion : 2.12.13Libs:-l${libdir}-lglib-2.0cflags:-i${includedir}/glib-2.0-i${libdir}/glib-2.0/include
Pkg-config–list-all
List all available packages, located in/usr/lib/pkgconfig, which are all kinds of. pc files. /usr/local/lib/pkgconfig The following libname.pc file, the new software will generally be installed. pc file, no can create it yourself, and set the environment variable Pkg_config_path look for. pc file path.
Using the Pkg-config –cflags parameter gives you the options you need at compile time, and the –libs parameter gives you the option to connect. For example, suppose a sample.c program uses the Glib library, it can be compiled like this:
$ Gcc-c ' pkg-config--cflags glib-2.0 ' sample.c
Then connect like this:
$ gcc sample.o-o sample ' pkg-config--libs glib-2.0 '
Or the above two steps can also be combined to take the next step:
$ gcc sample.c-o sample ' pkg-config--cflags--libs glib-2.0 '
You can see that because the Pkg-config tool is used to get the library's options, you can use the same compilation and connection commands, regardless of the directory in which the library is installed, to unify the compilation and connection interfaces.
There are two basic prerequisites for extracting the compilation and connection parameters of a library using the Pkg-config tool:
The <1> library itself must provide a corresponding. pc file at the time of installation. The library description does not support the use of the Pkg-config tool.
<2> Pkg-config must know where to look for this. pc file.
4. Environment variable Pkg_config_path
The environment variable Pkg_config_path is used to set the search path for the. pc file, and the pkg-config is searched in the order of the set path until the specified. pc file is found. In this way, the settings for the search path to the header file of the library are actually turned into settings for the. pc file search path.
After installing a library that needs to be used, such as glib, one is to copy the corresponding. pc files, such as glib-2.0.pc, to the/usr/lib/pkgconfig directory, and two by setting the environment variable Pkg_config_ Path adds the search paths to the glib-2.0.pc file.
Add the environment variable Pkg_config_path, which should be set as follows in bash:
$ export pkg_config_path=/opt/gtk/lib/pkgconfig: $PKG _config_path
You can perform the following command to check whether the/opt/gtk/lib/pkgconfig path is already set in the PKG_CONFIG_PATH environment variable:
$ echo $PKG _config_path
After this setting, other programs or libraries that use the glib library are compiled with Pkg-config know first to/opt/gtk/lib/pkgconfig this directory to find glib-2.0.pc (GTK + and other dependent libraries of the. pc files will also be copied here, and will be first searched here for their corresponding. pc files. After that, the compilation and connection parameters of the library can be extracted by Pkg-config for the program to be used in compiling and connecting.
It is also important to note that the settings for the environment variable are valid only for the current terminal window. If you arrive at a terminal window that does not have the above settings, Pkg-config will not find the newly installed glib-2.0.pc file, which may cause subsequent installations (such as the installation of ATK after glib) to fail.
In our installation scenario, the settings for GTK + and its dependent libraries are made using environment variables, so if you want to use the newly installed GTK + libraries after the system restarts, or a new terminal window is opened, you need to reset Pkg_config_path and ld_ as above. Library_path environment variables.
This method of using GTK + has a process of setting up the library before using it. It's a little cumbersome, but it's one of the safest ways to use the GTK + library, without any impact on the system's already existing programs that use the GTK + library, such as the GNOME desktop.
Brief introduction of the relationship between configure, Pkg-config and Pkg_config_path