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

所以第一步是为了解决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.: cannot open shared object file: No such file or directory

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

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

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

ln -s /usr/local/lib/libpcreposix.so. /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 ;
}

编译如下:

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)

glib源码安装使用方法的更多相关文章

  1. 源码安装nginx 方法二

    yum 仓库不能用大写字母 [root@oldboy conf.d]# gzip * 压缩当前目录下的所有文件 gzip ./* gzip . gzip./ # 关闭防火墙和selinux [root ...

  2. Mininet系列实验(一):Mininet使用源码安装

    1 实验目的 掌握Mininet使用源码安装的方法. 2 实验原理 Mininet 是一个轻量级软件定义网络和测试平台:它采用轻量级的虚拟化技术使一个单一的系统看起来像一个完整的网络运行相关的内核系统 ...

  3. mono-3.4.0 源码安装时出现的问题 [do-install] Error 2 [install-pcl-targets] Error 1 解决方法

    Mono 3.4修复了很多bug,继续加强稳定性和性能(其实Mono 3.2.8 已经很稳定,性能也很好了),但是从http://download.mono-project.com/sources/m ...

  4. 织梦dedecms源码安装方法

    织梦dedecms源码安装方法 第一步: 上传所有文件到空间 注意:(由于有很多人反应安装后首页样式都乱的,所以强烈要求安装到根目录,如:127.0.0.1 / www.xxx.com,或者二级域名也 ...

  5. 记录一个源码安装mysql5.6的方法

    https://www.jb51.net/article/118853.htm 如果之前源码安装过mysql5.6的话,卸载方法如下:rm -rf /var/lib/mysql/rm -rf /usr ...

  6. Django源码安装方法及创建启动项目

    一.源码安装方法 下载源码包:https://www.djangoproject.com/download/ 输入以下命令并安装: tar xzvf Django-X.Y.tar.gz # 解压下载包 ...

  7. nginx源码安装方法

    nginx源码安装方法 安装方法如下 1.安装nginx必要的源码依赖软件包. yum -y install gcc gcc-c++ automake pcre pcre-devel zlib zli ...

  8. linux源码安装软件的一般方法

    rhel系统貌似安装不了xmgrace,配置的时候居然说要那个M*tif库.百度了一下,需要openmotif库,然后用root账户想要用yum安装一下这个库,搞了好久没搞懂.后面搞明白了,原因竟是因 ...

  9. 国产龙芯服务器源码安装PostgreSQL数据库的方法

    1. 公司最近有一些国产化项目的需求, 要求在国产CPU的服务器上面安装pg数据库等. 2.. 但是差查了下中标麒麟的官网,在龙芯MIPS的操作系统包源里面仅有 postgreSQL 9.2 版本的r ...

随机推荐

  1. .net通用权限框架B/S (四)--DAL数据层以及数据接口

    数据层以及数据接口设计如下图(以g_orga组织机构和g_role角色)为例,这几个类可以通过.tt模版生成 设计参考学习http://www.cnblogs.com/hanyinglong/arch ...

  2. [译] 使用Using Data Quality Services (DQS) 清理用户数据

    SQL Server 2012 Data Quality Services (DQS)  允许你使用自己的知识库来清洗数据. 在本文中我会展示一个简单示例. 使用DQS清理步骤如下: A. 建立DQS ...

  3. JAVA 年轻代收集器 第九节

    JAVA 年轻代收集器  第九节 继续上一章所讲的,STW即GC时候的停顿时间,他会暂停我们程序中的所有线程.如果STW所用的时间长而且次数多的话,那么我们整个系统稳定性以及可用性将大大降低. 因此我 ...

  4. 内存管理之二——Cocos2d-x学习历程(六)

    1.工厂方法 工厂方法是程序设计中一个经典的设计模式,指的是基类中只定义创建对象的接口,将实际的实现推迟到子类中. CCObject* factoryMethod() { CCObject* ret ...

  5. HDU 2685 I won't tell you this is about number theory

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2685 题意:求gcd(a^m - 1, a^n - 1) mod k 思路:gcd(a^m - 1, ...

  6. Tomcat启动失败的解决方法

    在使用Tomcat的时候,经常会遇到启动失败的问题:解决方法:1.检查环境变量的配置,jdk的配置2.检查端口是否被占用. 关于环境变量的配置很容易搜到,如果按照网上的教程配置好了,但是还是启动失败的 ...

  7. 02-4. BCD解密(10)

    BCD数是用一个字节来表达两位十进制的数,每四个比特表示一位.所以如果一个BCD数的十六进制是0x12,它表达的就是十进制的12.但是小明没学过BCD,把所有的BCD数都当作二进制数转换成十进制输出了 ...

  8. Android 初步

    Android 四大组件: 1.Activity -------构建应用程序界面 /*2.Intent--------程序之间传输数据*/ 2.BroadcastReciever--------用来接 ...

  9. 编程策略类note

    2016-1-15 LOG LOG最重要的作用即是为程序出bug时调试提供思路, 一个自定义的log,需要有几个要素: 1. 时间,以知道哪些log是我们所需要的, 2. 标签,判断哪些log是属于哪 ...

  10. MBG 相关资源链接

    MyBatis Generator(MBG)相关资源链接 http://mbg.cndocs.tk/quickstart.html http://www.mybatis.tk/ http://git. ...