pkg-config Command Detailed
Returns the meta information for the installed library file.
Environment variable: Pkg_config_path. Pkg-config will look for all. pc files under this variable. In addition, it looks for the. pc files under/usr/lib/pkgconfig/.
Pkg-config works on multiple platforms:linux and other unix-like operating systems, Mac OS X and Windows. It does not require anything but a reasonably working-C compiler and a C library, but can use a installed if th At is present. (A copy of GLib 1.2.8 is shipped together with pkg-config and this is sufficient for pkg-config to compile and work Ly.)
After the library file is compiled, a directory named Pkgconfig is generated under {installed_home/lib/} that contains the. pc file for that link library. You need to copy the file to the/usr/lib/pkgconfig/directory or specify the path to the file in the environment variable. The purpose of this is to enable other programs that rely on this link library to automatically load the library with the help of pkg-config to avoid compile-time errors.
Parameter list
–list list all installed shared libraries
–cflags lists the preprocessing and compilation flag for the specified shared library.
–libs lists the link flag for the specified shared library.
First, compile and connect
Generally speaking, if the header file of the library is not in the/usr/include directory, then 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, user installation library can also be installed in different directories, so even with the same library, due to the different paths of the library, resulting in the use-I parameter specified header file path may also be different, the result is that the compiler command interface is not unified. If the-l parameter is used, the connection interface is also not uniform. The compilation and connection interface is not unified will bring trouble to the use of the library.
In order to solve the problem of the compilation and connection interface is not unified, people have found some solutions. The basic idea is to save the location information of the library in advance, and then to extract the useful information from it for compiling and connecting using the specific tools. In this way, you can do the compilation and connection interface consistency. Among them, the most commonly used library information extraction tool is described below Pkg-config.
Pkg-config is a. pc file that is provided through a library to obtain all the necessary information about the library, including version information, parameters for compiling and connecting, and so on. This information can be extracted separately from the parameters provided by the Pkg-config directly for use by compilers and connectors.
The Pkgconfig package contains tools for passing the include path and/or library paths to builds tools during the Make file Execution.
The Pkg-config is a function This 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 could add to Pkg_config_path by exporting additional paths on your system where pkgconfig files are. Note So Pkg_config_path is needed when compiling packages, not during run-time.
By default, the. pc file for each library that supports Pkg-config is installed in the Lib/pkgconfig directory in the installation directory. For example, we have already installed the Glib in the/OPT/GTK directory above, then the Glib library corresponds to the. pc file is a file named glib-2.0.pc in the/opt/gtk/lib/pkgconfig directory:
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
Using the Pkg-config--cflags parameter gives you the options you need at compile time, and--libs parameters give you the option of connecting. For example, suppose a sample.c program uses the Glib library to compile:
$ 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 merged to take the next step:
$ gcc sample.c-o sample ' pkg-config--cflags--libs glib-2.0 '
As you can see: With the Pkg-config tool used to get the options for the library, you can use the same compilation and connection commands regardless of the library installation, bringing a unified compilation and connection interface.
There are two basic prerequisites for using the Pkg-config tool to extract compilation and connection parameters for a library:
1. The library itself must provide a corresponding. pc file at the time of installation. Library descriptions that do not do this do not support the use of the Pkg-config tool.
2. Pkg-config must know where to look for this. pc file.
GTK + and its dependent libraries support the use of the Pkg-config tool, so the remaining question is how to tell Pkg-config where to find the corresponding. pc file, which is also resolved by setting the search path. For GTK + and its dependent libraries that support the Pkg-config tool, the setting of the search path for the header file of the library becomes the setting for the. pc file search path. The search path for the PC file is set by the environment variable Pkg_config_path, pkg-config Searches in the order of the set path until the specified. pc file is found.
After installing Glib, the following settings should be set 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 has been set in the PKG_CONFIG_PATH environment variable:
$ echo $PKG _config_path
After this setting, other programs or libraries that use the Glib library Pkg-config know that the first thing to do is to go to/opt/gtk/lib/pkgconfig directory to find glib-2.0.pc (GTK + and other dependent libraries. pc files will also be copied to this , they will also search for their corresponding. pc files first. After that, the compilation and connection parameters of the library can be extracted by Pkg-config for the program to use when compiling and connecting.
Also note that the settings for the environment variable are valid only for the current terminal window. If you are in 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 ATK installed after glib) to fail.
In our installation scenario, because of the setting of GTK + and its dependent libraries using environment variables, when the system restarts or a new terminal window is opened, if you want to use the newly installed GTK + library, you need to reset Pkg_config_path and ld_ as above Library_path environment variables.
This method of using GTK + is one more procedure for setting up a library before using it. Although somewhat cumbersome, it is one of the safest ways to use the GTK + library without any impact on the system's existing programs that use the GTK + library, such as the GNOME desktop.
To make the settings of the library simpler, you can save the following two sentences to a file (for example, 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
After that, you can use the following method to set up the library (where the source command can also be used.) instead):
$ source set_gtk-2.10
These settings are only necessary if you are developing an application using the new GTK + library, or if you are running a program that uses the new GTK + library.
If you want to avoid the trouble of using the same settings before the GTK + library, you can set the above two environment variables in the system's configuration file (such as/etc/profile) or your own user profile (such as ~/.bash_profile), and the library's search path can also be set in/etc/ ld.so.conf files, and so on. This setting takes effect when the system starts, which can lead to a program using GTK + to use the new GTK + Runtime library, which may cause problems. Of course, it is convenient to use this setting if you find that there is nothing wrong with using the new version of GTK + instead of the old version. Add to ~/.BASHRC, for example: After pkg_config_path=/opt/gtk/lib/pkgconfig reboot: [Root@localhost ~]# Echo $PKG _config_path/opt/gtk/lib /pkgconfig
Second, run time
Library files are used when connecting (static and shared libraries) and running (limited to programs that use shared libraries), and their search paths are set in the system. The General Linux system/lib and/usr/lib two directories as the default library search path, so use the libraries in both directories without having to set the search path to use directly. For libraries that are outside the default library search path, you need to add the location of the library to the library's search path. There are two ways to set the search path for a library file, which you can use either:
1. Specify the search path for the library in the environment variable Ld_library_path.
2. Add the search path for the library in the/etc/ld.so.conf file.
It's wise to add the path to your library file to/etc/ld.so.conf. Adding method is also extremely simple, the absolute path of the library file directly into the OK, one line. For example:/usr/x11r6/lib/usr/local/lib/opt/lib
Note that the second search path is set up in a way that is sufficient to locate libraries (including shared libraries and static libraries) when the program is connected, but not enough for programs that use shared libraries. This is because in order to speed up the execution of the program to the shared library, avoid using the search path to find the shared library inefficient, so it is directly read the library list file/etc/ld.so.cache from the search. /etc/ld.so.cache is a non text data file and cannot be edited directly, which is based on the search path set in/etc/ld.so.conf by/sbin/ldconfig command to centralize shared library files under these search paths (the Ldconfig command executes as root). Therefore, in order to ensure the location of the library when the program is executed, after setting the library search path in/etc/ld.so.conf, you must also run the/sbin/ldconfig command to update the/etc/ld.so.cache file. Ldconfig, to put it simply, is to cache the library files in the path listed/etc/ld.so.conf to/etc/ld.so.cache for use. So when you have installed some library files (for example, just install glib), or modify ld.so.conf add a new library path, you need to run the/sbin/ldconfig so that all the library files are cached to Ld.so.cache, if not done, even if the library file is clearly in the Under the Usr/lib, is also not used, the results of the compilation process in the wrong, the lack of XXX library, to see the discovery is clearly placed in there, engaged in the want to lambaste computer stupid pig. ^_^ the search path for library files (static libraries and shared libraries) is explicitly specified by the-l parameter, in addition to the settings above, when the program is connected. Because the path set with-l is first searched, it is usually in this way that the path of the library to which you want to connect is directly specified in the connection.
As explained earlier, there are two ways to set up a library search path: Set in the environment variable Ld_library_path and set in the/etc/ld.so.conf file. In this case, the second setting requires root permission to change the/etc/ld.so.conf file and execute the/sbin/ldconfig command. Also, when the system restarts, all GTK2 programs will use the newly installed GTK + library at run time. Unfortunately, because of the changes in the GTK + version, this sometimes brings compatibility problems to the application, causing some programs to run abnormally. To avoid these situations, the first way to set up the search path for a library during the installation of GTK + and its dependent libraries is to do so. This setting does not require root permissions and is simple to set:
$ export ld_library_path=/opt/gtk/lib: $LD _library_path
You can view the Ld_libray_path settings with the following command:
$ echo $LD _library_path so far, both of the library settings are complete.