A few days ago someone asked me how to upgrade the glib library and switch between versions. To install with RPM, the results found that the glib installation package does not support modifying the installation directory, such as.
This way, you can only specify the installation path when compiling the source code. In fact, I'm here to demonstrate the upgrade before installing the glib library, I don't need it myself, so don't ask me how to use the features provided by glib ~
First download the glib2.4, unzip to see what configuration options GLib provide (all say just the upgrade of the demo library, I do not want to fully install glib), such as:
[[email protected] glib-2.4.0]#./configure-help ' Configure ' configures glib 2.4.0 to adapt to many kinds of systems. Usage:./configure [OPTION] ... [Var=value] ... To assign environment variables (e.g., CC, CFLAGS ...), specify them asvar=value. See below for descriptions of some of the useful variables. Defaults for the options is specified in brackets. Configuration:-H,--help display this Help and exit--help=short display options specific to th Is package--help=recursive display the short help of all the included Packages-v,--version display V Ersion information and Exit-q,--quiet,--silent do not print ' checking ... ' Messages--cache-file=file cache Te St results in FILE [disabled]-C,--config-cache alias for '--cache-file=config.cache '-N,--no-create do n OT Create output files--srcdir=dir find the sources in dir [Configure DIR or ' ... '] Installation Directories:--prefix=prefix Install architecture-independent files in PREFIX [/usr/local]--exec-prefix=eprefix install architecture-dependent files in E PREFIX [prefix]by default, ' make install ' would install all of the files in '/usr/local/bin ', '/usr/local/lib ' etc. You can Specifyan installation prefix other than '/usr/local ' using '--prefix ', for instance '--prefix= $HOME '. For better control, use the options below. Fine tuning of the installation directories:--bindir=dir user executables [Eprefix/bin]--sbindir=dir System admin Executables [Eprefix/sbin]--libexecdir=dir program executables [eprefix/libexec]--datadir=dir Read-only architecture-independent data [prefix/share]--sysconfdir=dir read-only single-machine data [PREFIX /etc]--sharedstatedir=dir modifiable architecture-independent data [prefix/com]--localstatedir=dir modifiable sin Gle-machine data [Prefix/var]--libdir=dir object code libraries [Eprefix/lib]--includedir=dir C Header Files [prefix/include]--oldincludedir=dir C header files for NON-GCC [/usr/include]--infodir=dir info documen tation [Prefix/info]--mandir=dir man documentation [Prefix/man]program names:--program-prefix=prefix Prepend PREFIX to Installed program names--program-suffix=suffix append suffix to Installed program names --program-transform-name=program run SED program on installed program Namessystem types:--build=build Configure for building in build [guessed]--host=host cross-compile to BUILD programs to run on host [Build]optional Features :--disable-feature do not include FEATURE (same as--enable-feature=no)--enable-feature[=arg] include FEATURE [ Arg=yes]--enable-maintainer-mode enable make rules and dependencies not useful (and sometimes C onfusing) to the casual installer--enable-debug=[no/minimum/yes] turn on debugging [Default=min Imum]--enable-gc-friendly turn on garbage collector friendliness [Default=no]--disable-mem-pools Disable all glib memory pools--ENABL E-ansi turn on strict ANSI [Default=no]--enable-threads turn in basic thread support [Default=yes] ([=n O] would override--with-threads)--disable-rebuilds Disable all source autogeneration rules --disable-dependency-tracking Speeds up one-time builds--enable-dependency-tracking does not reject slow dependency ext Ractors--disable-largefile omit support for large files--enable-static[=pkgs] build Stati c libraries [Default=no]--enable-shared[=pkgs] Build shared libraries [Default=yes]--enable-f AST-INSTALL[=PKGS] Optimize for fast installation [Default=yes]--disable-libtool-lock avoid lo Cking (might break parallel builds)--enable-included-printf use included printf [Default=auto] --enable-gtk-doc Use Gtk-doc to build documentation default=no--enable-man regenerate mans pages from Docbook [Default=no]opti Onal Packages:--with-package[=arg] Use of the package [Arg=yes]--without-package Don't use the package (same as--with- Package=no)--with-libiconv=[no/gnu/native] Use the Libiconv library--with-gnu-ld as Sume the C compiler uses GNU ld [Default=no]--with-pic try to use only Pic/non-pic objects [Default=use Both]--with-tags[=tags] include additional configurations [automatic]-- WITH-THREADS=[NONE/POSIX/DCE/SOLARIS/WIN32] Specify a thread implementation to use--with-html-d Ir=path path to installed docs--with-xml-catalog=catalog path to XML catalog to Usesome INFL uential environment variables:cc C compiler command CFLAGS C compiler flags LDFLAGS linker flags, E. G.-l<lib dir> ifYou have libraries in a nonstandard directory <lib dir> cppflags C + + preprocessor flags, e.g.-i& Lt;include dir> If you had headers in a nonstandard directory <include dir> CXX C + + Compil ER command cxxflags C + + compiler flags CPP C preprocessor cxxcpp C + + preprocessor F77 Fortran compiler command fflags Fortran compiler flagsuse These variables to override the choices made by ' configure ' Or to Helpit to find libraries and programs with nonstandard names/locations. Report bugs to Through this list, you can choose to compile glib source code, and set the compilation parameters, my settings are as follows: Generate static libraries, do not optimize, generate mapping files./configure--prefix=/root/glib2.4--enable-debug=yes--enable-static cflags= "-g3-o0" LDFLAGS= "-Wl,-Map,Sym.map"
With good luck, make and make install process error, so glib2.4 done
The same approach was used on glib2.10, and eventually, I had two versions of the glib library on my computer:The purpose of the installation library is to use the functionality it provides in the project, so a test program test.c and Makefile is written here:
TEST.C:
#include <glib.h> #include <stdio.h>int main () {printf ("version:%d%d%d\n", Glib_major_version,glib_minor _version, glib_micro_version); return 0;}
Makefile:target=testobjs=test.oall:$ (OBJS) Gcc-g3-o0-o $ (TARGET) ' Pkg-config--libs glib-2.0 ' $ (OBJS)%.o:%.cgcc-g3-o0-c ' pkg-c Onfig--cflags glib-2.0 ' $<
Makefile uses the Pkg-config tool to manage the library version, which is the purpose of the free switch library. Here's a description of pkg-config from the Internet:The Pkg-config requires the library to provide a. PC metadata file that retrieves all the necessary information from these files, including version information, compilation
And the parameters needed for the connection. This information can be extracted separately from the parameters provided by Pkg-config (e.g.--cflags,--libs) directly
For use by compilers and connectors.
By default, each Pkg-config-enabled library corresponds to a. pc file, which is located in the directory/usr/lib/pkgconfig directory after installation
Under
Environment variable Pkg_config_path is used to set the search path for. pc files, pkg-config in the order of the set path
Search. In this way, the settings for the search path to the header file of the library are actually turned into settings for the. pc file search path. "
...
"In general, even with the same library, different users may be installed in different directories when they are installed." This is used at compile time
The-I parameter specifies the include path, which specifies the path of the Lib library when connecting using the-l parameter, which may cause compilation, inconsistent connections, and the same
Copy of the program from one machine to another may cause problems.
Pkg-config is a tool used to solve the problem of non-uniformity of the compiler interface.
Its basic idea: in advance to save all the necessary information of the library in the. pc file, you can use the parameters when needed (--cflags,
--libs) to extract the required information for compilation and connection use. This way, regardless of where the library files are installed, the. pc files that correspond to the libraries
Can be accurately located, you can use the same compilation and connection commands, so that the compilation and connection interface unified.
The main functions it provides are:
<1> Check the version number of the library. If the desired version of the library does not meet the requirements, print out the error message and avoid connecting to the wrong version of the library file.
<2> gets the path to the header file for the compilation preprocessing parameters, such as the macro definition.
<3> get compile parameters, such as the location of libraries and other libraries they depend on, filenames, and other connection parameters.
<4> automatically joins the settings of other libraries that depend on it. "
The big paragraph mentions the. pc file, which provides the parameters for compiling the connection, so take a look at the. pc files of the two versions of the glib library that I installed:
On the left is the lower version 2.4 to the right is the high version 2.10.
Finally, let's see how to switch between two versions at compile time:
Set the search path for the. pc file by setting the export Pkg_config_path=path to. PC and use the compile connection parameters in it.
If test uses a 2.4 library, set to:
Export pkg_config_path=/root/glib2.4/lib/pkgconfig/
Using the 2.10 library, set to:Export pkg_config_path=/root/glib2.0/lib/pkgconfig/
The result of the final operation is:
Different search paths, get different version information, which is sufficient to indicate that the test program is properly connected to the specified glib version. It feels like a polymorphic when compiling a connection
Upgrading and switching glib libraries using pkg-config