使用pkg-config升级和切换glib库

2024-02-29 01:48

本文主要是介绍使用pkg-config升级和切换glib库,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

 前几天有人问我如何升级glib库,并在各个版本之间切换。本想用rpm安装的,结果发现glib安装包不支持修改安装目录,如下图。

    

    如此,只能在编译源码时指定安装路径了。其实,我这是为了演示升级才安装glib库,我本身不需要它,所以不要问我如何使用glib提供的功能~

    首先下载了glib2.4,解压后看看glib提供了哪些配置选项(都说只是演示库的升级,我并不想完整安装glib),如下图:

   

[cpp]  view plain  copy
  1. [root@localhost glib-2.4.0]# ./configure -help  
  2. `configure' configures glib 2.4.0 to adapt to many kinds of systems.  
  3.   
  4. Usage: ./configure [OPTION]... [VAR=VALUE]...  
  5.   
  6. To assign environment variables (e.g., CC, CFLAGS...), specify them as  
  7. VAR=VALUE.  See below for descriptions of some of the useful variables.  
  8.   
  9. Defaults for the options are specified in brackets.  
  10.   
  11. Configuration:  
  12.   -h, --help              display this help and exit  
  13.       --help=short        display options specific to this package  
  14.       --help=recursive    display the short help of all the included packages  
  15.   -V, --version           display version information and exit  
  16.   -q, --quiet, --silent   do not print `checking...' messages  
  17.       --cache-file=FILE   cache test results in FILE [disabled]  
  18.   -C, --config-cache      alias for `--cache-file=config.cache'  
  19.   -n, --no-create         do not create output files  
  20.       --srcdir=DIR        find the sources in DIR [configure dir or `..']  
  21.   
  22. Installation directories:  
  23.   --prefix=PREFIX         install architecture-independent files in PREFIX  
  24.               [/usr/local]  
  25.   --exec-prefix=EPREFIX   install architecture-dependent files in EPREFIX  
  26.               [PREFIX]  
  27.   
  28. By default, `make install' will install all the files in  
  29. `/usr/local/bin', `/usr/local/lib' etc.  You can specify  
  30. an installation prefix other than `/usr/local' using `--prefix',  
  31. for instance `--prefix=$HOME'.  
  32.   
  33. For better control, use the options below.  
  34.   
  35. Fine tuning of the installation directories:  
  36.   --bindir=DIR           user executables [EPREFIX/bin]  
  37.   --sbindir=DIR          system admin executables [EPREFIX/sbin]  
  38.   --libexecdir=DIR       program executables [EPREFIX/libexec]  
  39.   --datadir=DIR          read-only architecture-independent data [PREFIX/share]  
  40.   --sysconfdir=DIR       read-only single-machine data [PREFIX/etc]  
  41.   --sharedstatedir=DIR   modifiable architecture-independent data [PREFIX/com]  
  42.   --localstatedir=DIR    modifiable single-machine data [PREFIX/var]  
  43.   --libdir=DIR           object code libraries [EPREFIX/lib]  
  44.   --includedir=DIR       C header files [PREFIX/include]  
  45.   --oldincludedir=DIR    C header files for non-gcc [/usr/include]  
  46.   --infodir=DIR          info documentation [PREFIX/info]  
  47.   --mandir=DIR           man documentation [PREFIX/man]  
  48.   
  49. Program names:  
  50.   --program-prefix=PREFIX            prepend PREFIX to installed program names  
  51.   --program-suffix=SUFFIX            append SUFFIX to installed program names  
  52.   --program-transform-name=PROGRAM   run sed PROGRAM on installed program names  
  53.   
  54. System types:  
  55.   --build=BUILD     configure for building on BUILD [guessed]  
  56.   --host=HOST       cross-compile to build programs to run on HOST [BUILD]  
  57.   
  58. Optional Features:  
  59.   --disable-FEATURE       do not include FEATURE (same as --enable-FEATURE=no)  
  60.   --enable-FEATURE[=ARG]  include FEATURE [ARG=yes]  
  61.   --enable-maintainer-mode enable make rules and dependencies not useful  
  62.                           (and sometimes confusing) to the casual installer  
  63.   --enable-debug=[no/minimum/yes]  
  64.                           turn on debugging [default=minimum]  
  65.   --enable-gc-friendly    turn on garbage collector friendliness [default=no]  
  66.   --disable-mem-pools     disable all glib memory pools  
  67.   --enable-ansi           turn on strict ansi [default=no]  
  68.   --enable-threads        turn on basic thread support [default=yes] ([=no]  
  69.                           will override --with-threads)  
  70.   --disable-rebuilds      disable all source autogeneration rules  
  71.   --disable-dependency-tracking Speeds up one-time builds  
  72.   --enable-dependency-tracking  Do not reject slow dependency extractors  
  73.   --disable-largefile     omit support for large files  
  74.   --enable-static[=PKGS]  
  75.                           build static libraries [default=no]  
  76.   --enable-shared[=PKGS]  
  77.                           build shared libraries [default=yes]  
  78.   --enable-fast-install[=PKGS]  
  79.                           optimize for fast installation [default=yes]  
  80.   --disable-libtool-lock  avoid locking (might break parallel builds)  
  81.   --enable-included-printf  
  82.                           use included printf [default=auto]  
  83.   --enable-gtk-doc        use gtk-doc to build documentation default=no  
  84.   --enable-man            regenerate man pages from Docbook [default=no]  
  85.   
  86. Optional Packages:  
  87.   --with-PACKAGE[=ARG]    use PACKAGE [ARG=yes]  
  88.   --without-PACKAGE       do not use PACKAGE (same as --with-PACKAGE=no)  
  89.   --with-libiconv=[no/gnu/native]  
  90.                           use the libiconv library  
  91.   --with-gnu-ld           assume the C compiler uses GNU ld [default=no]  
  92.   --with-pic              try to use only PIC/non-PIC objects [default=use  
  93.                           both]  
  94.   --with-tags[=TAGS]  
  95.                           include additional configurations [automatic]  
  96.   --with-threads=[none/posix/dce/solaris/win32]  
  97.                           specify a thread implementation to use  
  98.   --with-html-dir=PATH    path to installed docs  
  99.   --with-xml-catalog=CATALOG  
  100.                           path to xml catalog to use  
  101.   
  102. Some influential environment variables:  
  103.   CC          C compiler command  
  104.   CFLAGS      C compiler flags  
  105.   LDFLAGS     linker flags, e.g. -L<lib dir> if you have libraries in a  
  106.               nonstandard directory <lib dir>  
  107.   CPPFLAGS    C/C++ preprocessor flags, e.g. -I<include dir> if you have  
  108.               headers in a nonstandard directory <include dir>  
  109.   CXX         C++ compiler command  
  110.   CXXFLAGS    C++ compiler flags  
  111.   CPP         C preprocessor  
  112.   CXXCPP      C++ preprocessor  
  113.   F77         Fortran 77 compiler command  
  114.   FFLAGS      Fortran 77 compiler flags  
  115.   
  116. Use these variables to override the choices made by `configure' or to help  
  117. it to find libraries and programs with nonstandard names/locations.  
  118.   
  119. Report bugs to <http://bugzilla.gnome.org/enter_bug.cgi?product=glib>.  
通过这个列表,可以选择性的编译glib的源码,以及设置编译参数,我的设置如下:生成静态库,不优化,生成映射文件

[cpp]  view plain  copy
  1. ./configure --prefix=/root/glib2.4 --enable-debug=yes --enable-static CFLAGS="-g3 -O0" LDFLAGS="-Wl,-Map,Sym.map"  
由于运气好,make 和make install过程没有出错,于是glib2.4 done
同样的办法用在glib2.10上,最终,我的电脑上有了两个版本的glib库:

安装库的目的是为了在工程中使用它提供的功能,因此此处写了一个测试程序test.c及Makefile:

test.c:

[cpp]  view plain  copy
  1. #include <glib.h>  
  2. #include <stdio.h>  
  3. int main()  
  4. {  
  5.     printf("version:%d %d %d\n",GLIB_MAJOR_VERSION,GLIB_MINOR_VERSION, GLIB_MICRO_VERSION);  
  6.     return 0;  
  7. }  

Makefile:

[cpp]  view plain  copy
  1. TARGET=test  
  2. OBJS=test.o  
  3. all:$(OBJS)  
  4.     gcc -g3 -O0 -o $(TARGET) `pkg-config --libs glib-2.0` $(OBJS)  
  5. %.o:%.c  
  6.     gcc -g3 -O0 -c `pkg-config --cflags glib-2.0` $<  
Makefile中使用了pkg-config工具来管理库版本,做到自由切换库的目的。这里从网上摘抄一段对pkg-config的描述:

"pkg-config要求库提供一个.pc元数据文件,从这些文件中检索库的各种必要信息,包括版本信息,编译
和连接需要的参数等。这些信息可以通过pkg-config提供的参数(如--cflags, --libs)单独提取出来直接
供编译器和连接器使用。
默认情况下,每个支持pkg-config的库对应的.pc文件,在安装后,都位于目录/usr/lib/pkgconfig目录
下。
环境变量PKG_CONFIG_PATH是用来设置.pc文件的搜索路径的,pkg-config按照设置路径的先后顺序进行
搜索。这样,库的头文件的搜索路径的设置实际上就变成了对.pc文件搜索路径的设置。"

...

"一般来说,即使是使用同一个库,不同的用户在安装时也可能会安装在不同的目录下。这样在编译时使用
-I参数指定include路径,在连接时使用-L参数指定lib库的路径,可能造成了编译,连接的不一致,同一
份程序从一台机器copy到另一台机器时就可能会出现问题。
pkg-config就是用来解决编译连接界面不统一问题的一个工具。
它 的基本思想:事先把库的各种必要信息保存在.pc文件中,需要的时候可以使用参数(--cflags,
--libs),将所需信息提取出来供编译和连接使用。这样,不管库文件安装在哪,通过库对应的.pc文件就
可以准确定位,可以使用相同的编译和连接命 令,使得编译和连接界面统一。
它提供的主要功能有:
<1> 检查库的版本号。如果所需库的版本不满足要求,打印出错误信息,避免连接错误版本的库文件。
<2> 获得编译预处理参数,如宏定义,头文件的路径。
<3> 获得编译参数,如库及其依赖的其他库的位置,文件名及其他一些连接参数。
<4> 自动加入所依赖的其他库的设置。"

这大段话提到了.pc文件,提供了编译连接的参数,那来看看我安装的两个版本的glib库的.pc文件:


左边是低版本2.4右边是高版本2.10。

最后来看下如何在编译时实现两个版本之间切换:

通过设置export PKG_CONFIG_PATH=path to .pc设置.pc文件的搜索路径并使用其中的编译连接参数。

如test使用2.4的库,则设置为:

[cpp]  view plain  copy
  1. export PKG_CONFIG_PATH=/root/glib2.4/lib/pkgconfig/  

使用2.10的库,则设置为:

[cpp]  view plain  copy
  1. export PKG_CONFIG_PATH=/root/glib2.0/lib/pkgconfig/  
最终的运行结果为:



不同的搜索路径,得到不同的版本信息,这足以说明测试程序正确的连接了指定的glib版本。感觉这像是编译连接时的多态~

这篇关于使用pkg-config升级和切换glib库的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!


原文地址:
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.chinasem.cn/article/757223

相关文章

使用Nginx配置文件服务器方式

《使用Nginx配置文件服务器方式》:本文主要介绍使用Nginx配置文件服务器方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1. 为什么选择 Nginx 作为文件服务器?2. 环境准备3. 配置 Nginx 文件服务器4. 将文件放入服务器目录5. 启动 N

使用nohup和--remove-source-files在后台运行rsync并记录日志方式

《使用nohup和--remove-source-files在后台运行rsync并记录日志方式》:本文主要介绍使用nohup和--remove-source-files在后台运行rsync并记录日... 目录一、什么是 --remove-source-files?二、示例命令三、命令详解1. nohup2.

Qt之QMessageBox的具体使用

《Qt之QMessageBox的具体使用》本文介绍Qt中QMessageBox类的使用,用于弹出提示、警告、错误等模态对话框,具有一定的参考价值,感兴趣的可以了解一下... 目录1.引言2.简单介绍3.常见函数4.按钮类型(QMessage::StandardButton)5.分步骤实现弹窗6.总结1.引言

Python使用Reflex构建现代Web应用的完全指南

《Python使用Reflex构建现代Web应用的完全指南》这篇文章为大家深入介绍了Reflex框架的设计理念,技术特性,项目结构,核心API,实际开发流程以及与其他框架的对比和部署建议,感兴趣的小伙... 目录什么是 ReFlex?为什么选择 Reflex?安装与环境配置构建你的第一个应用核心概念解析组件

Qt中Qfile类的使用

《Qt中Qfile类的使用》很多应用程序都具备操作文件的能力,包括对文件进行写入和读取,创建和删除文件,本文主要介绍了Qt中Qfile类的使用,具有一定的参考价值,感兴趣的可以了解一下... 目录1.引言2.QFile文件操作3.演示示例3.1实验一3.2实验二【演示 QFile 读写二进制文件的过程】4.

spring security 超详细使用教程及如何接入springboot、前后端分离

《springsecurity超详细使用教程及如何接入springboot、前后端分离》SpringSecurity是一个强大且可扩展的框架,用于保护Java应用程序,尤其是基于Spring的应用... 目录1、准备工作1.1 引入依赖1.2 用户认证的配置1.3 基本的配置1.4 常用配置2、加密1. 密

WinForms中主要控件的详细使用教程

《WinForms中主要控件的详细使用教程》WinForms(WindowsForms)是Microsoft提供的用于构建Windows桌面应用程序的框架,它提供了丰富的控件集合,可以满足各种UI设计... 目录一、基础控件1. Button (按钮)2. Label (标签)3. TextBox (文本框

使用Vue-ECharts实现数据可视化图表功能

《使用Vue-ECharts实现数据可视化图表功能》在前端开发中,经常会遇到需要展示数据可视化的需求,比如柱状图、折线图、饼图等,这类需求不仅要求我们准确地将数据呈现出来,还需要兼顾美观与交互体验,所... 目录前言为什么选择 vue-ECharts?1. 基于 ECharts,功能强大2. 更符合 Vue

如何合理使用Spring的事务方式

《如何合理使用Spring的事务方式》:本文主要介绍如何合理使用Spring的事务方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1、介绍1.1、底层构造1.1.事务管理器1.2.事务定义信息1.3.事务状态1.4.联系1.2、特点1.3、原理2. Sprin

Vue中插槽slot的使用示例详解

《Vue中插槽slot的使用示例详解》:本文主要介绍Vue中插槽slot的使用示例详解,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录一、插槽是什么二、插槽分类2.1 匿名插槽2.2 具名插槽2.3 作用域插槽三、插槽的基本使用3.1 匿名插槽