Use of the command pkg-config tool in linux

Source: Internet
Author: User
Article Title: Use of the command pkg-config in linux. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.

1. 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_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:

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_PATHexport LD_LIBRARY_PATH =/opt/gtk/lib: $ LD_LIBRARY_PATH, you can use the following method to set the database (the source command can also be used. ):

$ 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

Pkg-config -- list-all

List all available packages in libname under/usr/lib/pkgconfig and/usr/local/lib/pkgconfig. pc files. New software is usually installed. pc file. You cannot create it yourself and set the environment variable PKG_CONFIG_PATH to search for it. pc file path.

Obtain some parameters, such

# Pkg-config -- cflags libpcsclite

-Lpthread-I/usr/local/include/PCSC

# Pkg-config -- libs libname

-L/usr/local/lib-lpcsclite

Other configurations

Ls/usr/bin/* config

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

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