Http://www.chenjunlu.com/2011/03/understanding-pkg-config-tool/
Http://www.mike.org.cn/articles/description-configure-pkg-config-pkg_config_path-of-the-relations-between/
1. What is configure?
In the source code installation process, mostly the configureProgramGenerally, configure is a script. During execution, you can input necessary parameters 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. cCCProgram. c`PKG-config-- Cflags -- LibsGnomeui` |
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}/IncludeGlib_genmarshal= Glib-genstmalGobject_query= Gobject-QueryGlib_mkenums= Glib-mkenums name: glibdescription: C utility libraryversion: 2.12.13libs:-L$ {Libdir}-Lglib-2.0Cflags:-I$ {Includedir}/G Lib-2.0-I$ {Libdir}/G Lib-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-- CflagsG Lib-2.0`Sample. c |
Then connect as follows:
? View code bash
1 |
$GccSample. o-OSample`PKG-config-- LibsG Lib-2.0` |
The preceding two steps can also be merged into the following steps:
? View code bash
1 |
$GccSample. c-OSample`PKG-config-- Cflags -- LibsG Lib-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 |
Have you developed software in Unix or Linux? After writing a program, the compilation and running are completely normal. It works well on your local machine.Source codeManagement System. Then, tell your colleagues that you can use it. At this moment, you have a long breath, a few days of work is not in vain, how fresh air ah, you began to float.
"Hi, how can I not compile it ?" You are still immersed in that wonderful feeling, your arms are full of strength, it seems that there is no problem to beat you. At this point, the idiot has shouted at you.
"No, I am doing well !" On the surface, you are very polite. In fact, you have already opened your mind. It's so stupid that you don't know how to use your mind. Maybe you're right. Last time, he made a simple mistake. Didn't you solve it as soon as you went.
Three times after he shouted, you had to let go of your work. The wonderful feeling just now has vanished without a trace. If it weren't for you to control your mood well, it's time for him to get angry. You enter make in front of your computer and press ENTER elegantly. How can an error be reported? You are confident. However, the results on the screen are somewhat blushing. Damn it, how can libxxx. So not be made?
You can search for libxxx. So in the/usr directory, and everything can't escape your eyes. Strange: How is libxxx. So stored in/usr/local/lib, not in/usr/lib? You can install it in the/lib directory next time.
The above scenario is not a fiction. I have already experienced it several times. It is clear that it is good on the local machine and cannot be compiled on other machines. The operating systems of the two users may be the same, and the required libraries must be installed in different directories due to their personal preferences. In this case, every time I had a hard time, I had to worry about whether I could work in other places.
The PKG-config we will introduce today provides a beautiful solution to solve the above problems. From then on, you no longer have to worry about it. PKG-config provides the following functions:
- Check the database version number. If the required library version does not meet the requirements, it prints an error message to avoid link to the library file of the wrong version.
- Obtain the pre-compilation parameters, such as macro definition and header file location.
- Obtain the link parameters, such as the location of the Library and other dependent libraries, file names, and other connection parameters.
- Automatically add the settings of other dependent libraries.
All of this is automatic. It doesn't matter where the library file is installed!
Before using PKG-config, let's talk about the principle of PKG-config. PKG-config is not a wizard and you can get the above information out of thin air. In fact, in order for PKG-config to obtain this information, the database provider is required to provide a. PC file. For example, the content of a PC file in GTK +-2.0 is as follows:
Prefix =/usr
Exec_prefix =/usr
Libdir =/usr/lib
Includedir =/usr/include
Target = X11
Gtk_binary_version = 2.4.0
Gtk_host = i386-redhat-linux-gnu
Name: GTK +
Description: gimp Tool Kit ($ {target} target)
Version: 2.6.7
Requires: gdk-$ {target}-2.0 ATK
Libs:-L $ {libdir}-lgtk-$ {target}-2.0
Cflags:-I ${includedir}/gtk-2.0
This file is usually stored in/usr/lib/pkgconfig/or/usr/local/lib/pkgconfig/. Of course, it can be stored anywhere else, for example, the PC files related to X11 are stored in/usr/x11r6/lib/pkgconfig. In order for pkgconfig to find your PC file, you need to set the path of the PC file in the environment variable pkg_config_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:
$ 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_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 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.
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 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 (for example, you have just installed glib or modified 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. (^_^)
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
Finally, let me summarize, pkg_config_path mainly specifies. the path of the PC file, so that the PKG-config tool can follow. the content of the PC file dynamically generates compilation and connection options, such as cflags (for compilation) and libs (for connection). If dynamic link library is used, then the program is connected and running, in Linux, the/lib and/usr/lib directories are used as the default library search path, the system administrator can set the LD_LIBRARY_PATH environment variable or in/etc/lD. so. the search path of the added library in the conf file. It is worth noting that, if you do not use the PKG-config tool, you need to display the dynamic link library name for the connection when using gcc. You can view the following precautions when using gcc.
In Linux, to allow the dynamic link library to be shared by other programs in the system, the name must comply with the format lib *. So. If a dynamic link library does not conform to this format, the Linux dynamic link library automatically loads the program (LD) and cannot find this link library, and other programs cannot share it. In the format, the first * is usually abbreviated as the database name, and the second * is usually represented as the version number of the database. For example, in my system, the name of the basic C dynamic link library is libc. so.6, and the name of the thread pthread dynamic link library is libpthread. so.0. If no version number is specified, for example, libmy. So, this is also the required format.
Important GCC commands:
- -Shared this option specifies to generate a dynamic Connection Library (let the connector generate a T-type export symbol table, sometimes also generate a weak connection W type export symbol, without this sign external programs cannot connect. Is equivalent to an executable file ).
- -FPIC: indicates that the compilation is in a separate position.CodeWithout this option, the compiled code is location-related. Therefore, during dynamic loading, the code is copied to meet the needs of different processes, rather than truly sharing code segments.
- -L.: indicates that the database to be connected is in the current directory.
- -Lmy: an implicit naming rule is used by the compiler to search for a dynamic connected library. That is, the Library name (libmy. So) is determined by adding lib before the given name and. So ).
If you have the root permission, you can modify/etc/lD. so. CONF file, and then call/sbin/ldconfig to achieve the same purpose. However, if you do not have the root permission, you can only use the LD_LIBRARY_PATH output method.