Briefly describe the relationship among configure, PKG-config, and pkg_config_path.

Source: Internet
Author: User
Tags gtk

Conversion from: briefly describes the relationship among configure, PKG-config, and pkg_config_path.

  1. What is configure?

In the source code installation process, the configure program is mostly used. Generally, configure is a script, and necessary parameters can be input during execution to inform the configuration project.

The Configure program checks the environment on which the program is compiled and installed based on the input configuration items, generate the MAKEFILE file required for compilation for the program make to read and use, and then call the relevant Compilation Program (usually calling the Compilation Program is GCC) to compile the final binary program. When the configure script checks the corresponding dependent environment (for example, the software version and the corresponding library version), it usually uses the PKG-config tool to detect the corresponding dependent environment.

  2. What is PKG-config?

1. Introduction to PKG-config

PKG-config is used to retrieve information about the installed library files in the system. Typically, it is used for library compilation and connection. For example, in makefile:

? View code bash
123
program: program.c cc program.c `pkg-config --cflags --libs gnomeui`

2. PKG-config Function

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 be different from the path of the Lib library specified by the-l parameter during connection, the result is that the compilation command interface is inconsistent. The compilation and connection inconsistency may cause problems when the same program is copied from one machine to another.

PKG-config is a tool used to solve the problem of inconsistent compiling connection interfaces.

Its basic idea: PKG-config is A. PC file provided by the database to obtain various necessary information about the database, including version information, parameters required for compilation and connection. When needed, you can use the parameters (-cflags,-libs) provided by PKG-config to extract the required information for compilation and connection. In this way, no matter where the library file is installed, the. PC file corresponding to the Library can be accurately located, and the same compilation and connection commands can be used to unify the compilation and connection interfaces.

It provides the following functions:

<1> check the database version number. If the required library version does not meet the requirements, print the error message to avoid connecting to the library file of the wrong version.
<2> obtain the compilation preprocessing parameters, such as macro definition and header file path.
<3> obtain compilation parameters, such as the location of the database and other dependent libraries, file names, and other connection parameters.
<4> automatically add the settings of other dependent libraries.

3. Example of the. PC file in glib-2.0
  
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:

? View code bash
1234567891011121314
prefix=/opt/gtk/exec_prefix=${prefix}libdir=${exec_prefix}/libincludedir=${prefix}/include glib_genmarshal=glib-genmarshalgobject_query=gobject-queryglib_mkenums=glib-mkenums Name: 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 in the/usr/lib/pkgconfig Directory, which contains various. PC files. The libname. PC file under/usr/local/lib/pkgconfig. The. PC file is usually installed in new software. You cannot create it yourself, and set the environment variable pkg_config_path to find the. PC file path.
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:

? View code bash
1
$ gcc -c `pkg-config --cflags glib-2.0` sample.c

Then connect as follows:

? View code bash
1
$ gcc sample.o -o sample `pkg-config --libs glib-2.0`

The preceding two steps can also be merged into the following steps:

? View code bash
1
$ 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:

<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.

4. Environment Variable pkg_config_path

The environment variable pkg_config_path is used to set the search path for the. PC file. PKG-config searches by path until the specified. PC file is found. In this way, the search path of the header file of the library is actually changed to the search path setting for the. PC file.

After installing a required library, such as glib. PC files, such as glib-2.0.pc copies to the/usr/lib/pkgconfig directory, the second is to add the search path for the glib-2.0.pc file by setting the environment variable pkg_config_path.

Add the environment variable pkg_config_path. In bash, perform the following settings:

? View code bash
1
$ 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:

? View code bash
1
$ 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 ):

? View code bash
12
export PKG_CONFIG_PATH=/opt/gtk/lib/pkgconfig:$PKG_CONFIG_PATHexport LD_LIBRARY_PATH=/opt/gtk/lib:$LD_LIBRARY_PATH

The environment variable LD_LIBRARY_PATH is mainly the search path for adding a new installation library. Then, you can use the following method to set the database (the source command can also be replaced ):

? View code bash
1
$ 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:

? View code bash
1
PKG_CONFIG_PATH=/opt/gtk/lib/pkgconfig

After restart:

? View code bash
12
[[email protected] ~]# echo $PKG_CONFIG_PATH/opt/gtk/lib/pkgconfig

Iii. Connection to the runtime Database

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 the libraries in these two 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:

? View code bash
123
/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. In short, ldconfig 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 the 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 may cause compatibility issues to applications and cause 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:

? View code bash
1
$ export LD_LIBRARY_PATH=/opt/gtk/lib:$LD_LIBRARY_PATH

Run the following command to view the setting content of ld_libray_path:

? View code bash
1
$ echo $LD_LIBRARY_PATH

Now, the two settings of the database are complete.

  Iv. References
  
LD. So. conf file and pkg_config_path variable
Use PKG-config

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.