Use of the command PKG-config tool in Linux

Source: Internet
Author: User
Tags gtk

1. Compilation and Connection

In general, if the header file of the library is not in the/usr/include directory, you need to use-I
The parameter specifies its path. 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 the library path
The path of the header file specified by the-I parameter may be different. The result is that the compiling command interface is inconsistent. If-L 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 use specific tools to save the useful information.
Information is extracted 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
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 after installation.
Directory. For example, if we have installed glib in the/opt/GTK directory, the. PC file corresponding to this glib library is
The/opt/GTK/lib/pkgconfig directory contains a file named glib-2.0.pc:

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

  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 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. PC
File Search Path settings .. The search path of the PC file is set through the Environment Variable pkg_config_path, PKG-config
The search will be performed 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, other programs or libraries using the glib library will know that the first step is
/Opt/GTK/lib/pkgconfig this directory to find the glib-2.0.pc (GTK + and other dependent library. PC)
Files will also be copied here, and the corresponding. PC files will be searched here first ). Then, use PKG-config
You can 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 solution, the environment variable is used for GTK +
So when the system is restarted or a new terminal window is opened, if you want to use the newly installed GTK + library, you need to reset it as above
Pkg_config_path and LD_LIBRARY_PATH environment variables.

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, you can set the above two environment variables in the system configuration file (such
/Etc/profile) or your own user configuration file (such ~ /. Bash_profile); the library search path can also be set in
/Etc/lD. So. conf file, and so on. This setting takes effect when the system is started, which causes GTK + programs to use the new version of GTK +
Runtime Library, which may cause some problems. Of course, if you find that the new version of GTK +
If there is no problem with replacing the old version, 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. Linux
The system switches/lib and/usr/lib
The two directories are used as the default library search path. Therefore, you can directly use these directories without setting the search path. For libraries outside the default library search path, you must add the location of the database
In the search path of the database. 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
To speed up the positioning of shared libraries during program execution and avoid the inefficiency of searching for shared libraries using search paths, you can directly read the library list file/etc/lD. So. cache.
From which to search. /Etc/lD. So. cache is a non-text data file and cannot be edited directly. It is based on/etc/lD. So. conf
The/sbin/ldconfig command is used to centralize the shared library files in these search paths.
Permission execution ). Therefore, to ensure that the database is located during program execution, you must run the database search path setting in/etc/lD. So. conf.
The/sbin/ldconfig command can be used to update the/etc/lD. So. cache file. Ldconfig
To put it simply, it 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 (for example, just installed Glib), or modifying lD. So. conf to add a new library path, run
/Sbin/ldconfig caches all library files to lD. So. cache. If it is not done, it will not be used even if the library files are clearly under/usr/lib.
In the compilation process, the result is wrong. If the xxx library is missing, you can check it and find that it is clearly there. 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
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
/Sbin/ldconfig command. In addition, after the system is restarted, all gtk2-based programs will use the newly installed GTK + Library at runtime. Unfortunately
GTK + version changes, which sometimes bring compatibility issues to applications, resulting in some program running exceptions. To avoid the above situations
The first method is used to set the search path of the library during the installation of the dependent library. 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.

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.