glib源码安装使用方法

glib库是GTK+和GNOME工程的基础底层核心程序库,是一个综合用途的实用的轻量级的C程序库,它提供C语言的常用的数据结构的定义、相关的处理函数,有趣而实用的宏,可移植的封装和一些运行时机能,如事件循环、线程、动态调用、对象系统等的API。它能够在类UNIX的操作系统平台(如LINUX、HP-UNIX等)、WINDOWS、OS2和BeOS等操作系统台上运行。

本文将介绍在linux下源码安装glib库的过程,这过程很麻烦,一点都不轻松,故记录下。

------

1、安装glib

http://ftp.acc.umu.se/pub/GNOME/sources/glib/

我下载了个glib-2.48.1.tar.xz,如果是.tar.xz格式用tar -xvf解压,如果是.tar.gz格式用tar -zxvf解压

解压后进入目录后,三部曲:

./configure
make
make install

看起来是简单,但第一步./configure问题多多,诸多问题请看下面各种解决法子。

 

2、zlib问题

报错如下:

configure: error: *** Working zlib library and headers not found ***

自glib-2.23开始就需要zlib,zlib是提供数据压缩用的函式库。

http://www.zlib.net/ (下载地址在网页的中间部分)

我下载了个zlib-1.2.8.tar.gz,解压、进目录,三部曲:

./configure
make
make install

 

3、libffi问题

报错如下:

No package 'libffi' found

Consider adjusting the PKG_CONFIG_PATH environment variable if you
installed software in a non-standard prefix.

Alternatively, you may set the environment variables LIBFFI_CFLAGS
and LIBFFI_LIBS to avoid the need to call pkg-config.
See the pkg-config man page for more details.

"FFI" 的全名是Foreign Function Interface,通常指的是允许以一种语言编写的代码调用另一种语言的代码。而libffi库只提供了最底层的、与架构相关的、完整的"FFI",在它之上必须有一层来负责管理两种语言之间参数的格式转换。

http://sourceware.org/libffi/

我下载了libffi-3.2.1.tar.gz,解压、进目录,三部曲:

./configure
make
make install

 

4、pkg-config问题

报错如下:

configure: error: The pkg-config script could not be found or is too old.  Make sure it is in your PATH or set the PKG_CONFIG environment variable to the full path to pkg-config.

pkg-config能根据软件安装时软件的.pc配置文件路径找到相应的头文件路径和库文件路径。

https://www.freedesktop.org/wiki/Software/pkg-config/

我下载了pkg-config-0.29.1.tar.gz,解压、进目录,三部曲:

./configure
make
make install

如果第一步遇到如下错误:

configure: error: Either a previously installed pkg-config or "glib-2.0 >= 2.16" could not be found. Please set GLIB_CFLAGS and GLIB_LIBS to the correct values or pass --with-internal-glib to configure to use the bundled copy.

只需要:

./configure --with-internal-glib

 

5、pcre问题

报错如下:

configure: error: Package requirements (libpcre >= 8.13) were not met:

No package 'libpcre' found

Consider adjusting the PKG_CONFIG_PATH environment variable if you
installed software in a non-standard prefix.

Alternatively, you may set the environment variables PCRE_CFLAGS
and PCRE_LIBS to avoid the need to call pkg-config.
See the pkg-config man page for more details.

PCRE(Perl Compatible Regular Expressions)是一个Perl库,包括perl兼容的正则表达式库。这些在执行正规表达式模式匹配时用与Perl 5同样的语法和语义是很有用的,也可以来解决C语言中使用正则表达式的问题。

https://sourceforge.net/projects/pcre/files/pcre/

我下载了pcre-8.38.tar.gz,解压、进目录,改动的三部曲:

./configure --enable-utf8 --enable-unicode-properties
make
make install

如果第一步只是./configure,到时候安装glib会继续报错:

checking for Unicode support in PCRE... no
configure: error: *** The system-supplied PCRE does not support Unicode properties or UTF-8.

所以第一步是为了解决Unicode、UTF-8的支持,仅限于pcre7.9以上版本

*安装pcre如果报错如下:

configure: error: You need a C++ compiler for C++ support

那是你所用的linux没支持c++编译,只需要:

apt-get install build-essential

**当安装好pcre后,输入pcretest -C来打印pcre的安装情况,一般输出如下:

PCRE version 8.38 2015-11-23
Compiled with
  8-bit support
  UTF-8 support
  Unicode properties support
  No just-in-time compiler support
  Newline sequence is LF
  \R matches all Unicode newlines
  Internal link size = 2
  POSIX malloc threshold = 10
  Parentheses nest limit = 250
  Default match limit = 10000000
  Default recursion depth limit = 10000000
  Match recursion uses stack

而如果报错为:

pcretest: error while loading shared libraries: libpcre.so.1: cannot open shared object file: No such file or directory

pcretest: error while loading shared libraries: libpcreposix.so.0: cannot open shared object file: No such file or directory

这种情况好像大多数linux都会发生,只需要自己建立一个软链接即可:

ln -s /usr/local/lib/libpcre.so.1 /lib

ln -s /usr/local/lib/libpcreposix.so.0 /lib

(如果之后还没用,可以考虑把/lib换/lib64)

 

6、glib使用方法

*如果都按照上面所列举的种种问题仔细折腾了后还有问题的话,请留言给我。(本人在Kali2、Kali Rolling、Ubuntu等都测试过了)

现在写一个简单的c语言代码,命名为hello.c

#include <glib.h>
int main(int argc, char** argv){
    GList* list=NULL;
    list=g_list_append(list,"Hello world!");
    list=g_list_append(list,"made by pcat");
    list=g_list_append(list,"http://pcat.cnblogs.com");
    printf("The first item is %s\n",g_list_first(list)->data);
    return 0;
}

编译如下:

gcc $(pkg-config --cflags --libs glib-2.0) hello.c -o hello

./hello

如果在一些linux上报错:

undefined reference to `g_list_append'
undefined reference to `g_list_first'

那是因为有些gcc存在编译参数的顺序问题,$(pkg-config --cflags --libs glib-2.0)放在源文件的前面,而当编译器在源文件中遇到不能解析的函数时,在源文件之后的选项中寻找相关的信息,那么就出现了编译错误,也就是无法找到相关的函数定义。

所以只需要:

gcc hello.c -o hello $(pkg-config --cflags --libs glib-2.0)

 

posted @ 2016-05-23 15:56  pcat  阅读(46613)  评论(14编辑  收藏  举报