How to view GTK version PKG usage

Source: Internet
Author: User
    • PKG-config
    • View the module version
    • Modify

PKG-config

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, assume thatProgramThe glib library can be compiled 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:

 
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.

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

 
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.

Add the paths that may store the library files to/etc/lD. so. in the conf file, it is wise to select and add a method that is extremely simple. It is okay to directly write the absolute path of the library file into it, one line at a time. 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. When the program is connected, you can specify the search path of the library file (static library and shared library) In addition to the above settings, you can also use the-l parameter to explicitly specify. 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.

You can view the GTK version in the mail list by using the following methods:

1. Use External commands to view installed GTK library versions

$ PKG-config -- modversion GTK + (view version 1.2.x)

$PKG-config -- modversion GTK +-2.0 (view version 2.x)

$ PKG-config -- version (View the version of PKG-config)

$ PKG-config -- list-All | grep GTK (check whether GTK is installed) 

2. Use the DL library to view the symbolic links in the Dynamic Link Library:

#Include <Dlfcn.H>
#Include <Stdio.H>
Int
Main(Void)
{
Void *GTK;
Int *Maj, *Min, *Mic;
IntStatus=1;

GTK = Dlopen ( Libgtk-x11-2.0.so" , Rtld_lazy ) ;
If ( ! GTK )
Return 1 ;
Maj = Dlsym ( GTK , "Gtk_major_version" ) ;
Min = Dlsym ( GTK , "Gtk_minor_version" ) ;
Mic = Dlsym ( GTK , "Gtk_micro_version" ) ;
If ( Maj & & Min & & Mic ) {
Printf ( "% D. % d. % d \ n" , * Maj , * Min , * Mic ) ;
Status = 0 ;
}
Dlclose ( GTK ) ;
Return Status ;
}


3. If you want to check the version when running the GTK program, you can use the symbolic constant defined in the GTK library.

Extern const guint gtk_major_version;
Extern const guint gtk_minor_version;
Extern const guint gtk_micro_version;
Extern const guint gtk_binary_age;
Extern const guint gtk_interface_age;
Gchar * gtk_check_version (guint required_major,
Guint required_minor,
Guint required_micro );

# Define gtk_major_version
# Define gtk_minor_version
# Define gtk_micro_version
# Define gtk_binary_age
# Define gtk_interface_age
# Define gtk_check_version (Major, minor, micro) 

Modify PKG-config

Provides include for installed packages and outputs and Management of location compilation options for actual library installation.

When you need to use some packages, you may need to enter the path containing the header file and the location of the library. Because some libraries and their header files are not necessarily installed in the system's default repository and header files.

Using PKG-config to indicate different parameters and library names, it provides GCC with compilation options for your library.

For example, when I enter
Johnman @ johnman-desktop:/$ PKG-config -- cflags glib-2.0
Output
-I/usr/include/glib-2.0-I/usr/lib/glib-2.0/include
This option looks like the path indication of the GCC header file

All databases can be managed in a unified manner through PKG-config, which stores information through configuration files. These configuration files are suffixed with. PC. My system stores these files in the/usr/lib/pkgconfig/directory.

View the glib-2.0.pc under this directory
The file content is as follows:
########################
Prefix =/usr
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.11
Libs:-L ${libdir}-lglib-2.0
Cflags:-I $ {includedir}/glib-2.0-I $ {libdir}/glib-2.0/include
########################

Assume that the cflags option of PKG-config is the last line of output-I $ {includedir}/glib-2.0-I $ {libdir}/glib-2.0/include

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.