ubuntu完整安装glib心得

Posted andylauren

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ubuntu完整安装glib心得相关的知识,希望对你有一定的参考价值。

         学习完数据结构,感觉对数据的存储有了一种新的认识,在动态内存的基础上,链表有了绝对的灵活性,可以给未知长度的数据处理带来方便,但是在不停的写代码的过程中,感觉到了深深的仇恨,每次要实现功能,几乎源代码都有改动,也就是说每个使用链表的方法几乎都不一样,这就对自己编写的链表、队列、和栈的函数维护成本增加,甚至N天以后不知道每个版本的实际差别。
        这是我就在想是不是linux能够提供现成的链表实现呢,毕竟在内核中大量的实现链表的神乎其神的源代码,会不会好心的封装成库给其他人使用呢?很可惜linux没有做,所以基本上要使用链表以及队列都需要自己去实现源码,对于我这样一个懒癌重度患者这是不能忍受的,我就在网上找相关的资料,终于让我发现了第三方glib库。这个库的功能很强大,从小的g_uint8类型的定义,到g_thread线程的实现,这个库都可做到,下面简单介绍一下,完整的功能说明自己去百度吧。
glib介绍
glib库是Linux平台下最常用的C语言函数库,它具有很好的可移植性和实用性。
1 类型定义 
      整数类型. 布尔类型. 字符型. 浮点型. 指针. gconstpointer对于于标准C的const void*
2 glib宏 
    常用宏 
    整型与指针类型间的转换
    调试宏
    前提条件检查,断言, 判断构建是否是指定的构件
3 常用函数
4 内存管理
5 字符串处理
    字符串操作, 修改字符串, 字符串转换, 其他字符串转换函数
6 数据结构
    链表, 树, 哈希表, 
7 GString
    GString类似于标准C的字符串类型,但是GString能够自动增长,这些特性可以防止程序中的缓冲区溢出。下面是GString的定义:
8.计时器函数
9 错误处理函数
10 其它实用函数

基本上能够用的上的功能这个库都有实现

好了下面进入正题,一起来对hello world进行编译。

首先介绍开发环境

PC:XP sp3,深度完美 纯净标准版
虚拟机: VMware® Workstation,版本:
9.0.2 build-1031769
ubuntu:华清远见   开学版 12.04

今天的目标是把程序员的标准入门程序编译通过,也就是程序员连撩妹都会首先使用的“hello world”,功能就是使用g_printf函数打印。
源代码如下:
#include <stdio.h>
#include "glib.h"
int main(int agrc, char **argv)

    g_printf("Hello world!\\n");
    return 0;
今天的目标就是把这个程序编译通过并运行。

首先上来就是啥也不管,先写好代码,gcc再说,万一通过了呢,这种事情发生已经见怪不怪了。
linux@ubuntu:~/16021$ gcc hello.c 
hello.c:3:18: fatal error: glib.h: No such file or directory
compilation terminated.

很不幸,报错说没有头文件glib.h,但是也不意外,因为glib是第三方的库,如果没有安装是不会有库文件的。
下面有目标的了,安装第三方库文件glib,首先下载glib源码,因为apt中没有找到直接的安装,我下载的是glib-2.0.6.tar.gz,其他版本没有试验,查到资料说glib有个依赖libffi-3.2.1.tar.gz这个库,下载,想办法弄到ubuntu里面去,然后tar解压

需要先安装 libffi-3.2.1.tar.gz库
解压
linux@ubuntu:~/16021$ tar -xzvf libffi-3.2.1.tar.gz 
进入libffi-3.2.1文件夹,进行经典三部曲./configure   make   make install
我为了防止权限不足的以外,这三步都加了sudo权限
这个库安装的很快,之后同样动作对待
glib-2.0.6.tar.gz。

正常应该不会出现错误,这就基本上成功了一半。


 这是就可以在/usr/local/lib和/usr/local/include文件夹中找到glib2.0的代码,这时候再来编译下试试。

居然还说我没有头文件?原来是我们使用的头文件用了<>,会在环境变量设置的头文件目录查找头文件,目录是/usr/include 目录
,好了,知道了原因就好解决了,正常应该是在make中加入glib的头文件路径,但是我比较懒,而且现在也不太实用makefile,决定把头文件全部拷贝到/usr/include目录中,这种操作是不被推荐的,但是确实可以解决这个问题。
 makefile的方法我还没有研究。下面讲解一下复制文件的操作。
 

linux@ubuntu:~/16021$ sudo cp -r /usr/local/include/glib-2.0/* /usr/include/
好了,现在编译一下。
linux@ubuntu:~/16021$ gcc hello.c 
In file included from /usr/include/glib/galloca.h:30:0,
                 from /usr/include/glib.h:30,
                 from hello.c:3:
/usr/include/glib/gtypes.h:30:24: fatal error: glibconfig.h: No such file or directory
compilation terminated.
好了,终于是另外一个错误了,找不到glibconfig.h文件,来找找看。 这个文件在/usr/local/lib/glib-2.0/include这个目录里。 好的,老规矩,复制 sudo cp /usr/local/lib/glib-2.0/include/glibconfig.h /usr/include/ 现在编译一下
 linux@ubuntu:~/16021$ gcc hello.c 
/tmp/cctK6e9Q.o: In function `main':
hello.c:(.text+0x11): undefined reference to `g_printf'
collect2: ld returned 1 exit status

漂亮,现在文件问题已经没有了,开始说找不到函数。咋回事呢?库函数往往都需要编译时指定库文件名,来加上试试
gcc hello.c -o hello -lglib-2.0

 linux@ubuntu:~/16021$ gcc hello.c -o hello -lglib-2.0
/tmp/ccXsXTRK.o: In function `main':
hello.c:(.text+0x11): undefined reference to `g_printf'
collect2: ld returned 1 exit status
还是找不到,上课的时候好像是讲过,自定义的库如何添加到环境变量中去,有3种方法,其中有2种可以关机不消失。一种是将库文件加入到系统库目录中,一种是在/etc/ld.so.conf.d/目录中加入.conf文件,这两种方法都可以,我这里使用一下编写.conf文件的方式, 在 /etc/ld.so.conf.d/目录下新建glib.conf文件,文件中写入/usr/local/lib/,这是glib的库文件目录。 保存之后使用sudo ldconfig命令,现在在编译一下。
我开始怀疑是不是库没有安装上,上网查如何查看库的安装情况,网上给的结果是dpkg -l|grep glib命令,

网上说一个库应该有-data和-dev文件,我发现没有-dev文件,下面安装一下,谢天谢地apt有这个安装的, 命令是,sudo apt-get install libglib2.0-dev  安装过程中有个地方要y一下。
安装过程中可能会失败,要求更新软件,然后就是各种更新不了,由于天朝局域网资源实在有限,所以只能想起他办法解决,我发现我的
/etc/apt/sources.list文件可以正常更新,估计是里面的镜像地址的问题。下面列一下文件内容
######################################################################################################

 # deb cdrom:[Ubuntu 12.04.1 LTS _Precise Pangolin_ - Release i386 (20120817.1)]/ precise main restricted

# See http://help.ubuntu.com/community/UpgradeNotes for how to upgrade to
# newer versions of the distribution.
deb http://cn.archive.ubuntu.com/ubuntu/ precise main restricted
deb-src http://cn.archive.ubuntu.com/ubuntu/ precise main restricted

## Major bug fix updates produced after the final release of the
## distribution.
deb http://cn.archive.ubuntu.com/ubuntu/ precise-updates main restricted
deb-src http://cn.archive.ubuntu.com/ubuntu/ precise-updates main restricted

## N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu
## team. Also, please note that software in universe WILL NOT receive any
## review or updates from the Ubuntu security team.
deb http://cn.archive.ubuntu.com/ubuntu/ precise universe
deb-src http://cn.archive.ubuntu.com/ubuntu/ precise universe
deb http://cn.archive.ubuntu.com/ubuntu/ precise-updates universe
deb-src http://cn.archive.ubuntu.com/ubuntu/ precise-updates universe

## N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu 
## team, and may not be under a free licence. Please satisfy yourself as to 
## your rights to use the software. Also, please note that software in 
## multiverse WILL NOT receive any review or updates from the Ubuntu
## security team.
deb http://cn.archive.ubuntu.com/ubuntu/ precise multiverse
deb-src http://cn.archive.ubuntu.com/ubuntu/ precise multiverse
deb http://cn.archive.ubuntu.com/ubuntu/ precise-updates multiverse
deb-src http://cn.archive.ubuntu.com/ubuntu/ precise-updates multiverse

## N.B. software from this repository may not have been tested as
## extensively as that contained in the main release, although it includes
## newer versions of some applications which may provide useful features.
## Also, please note that software in backports WILL NOT receive any review
## or updates from the Ubuntu security team.
deb http://cn.archive.ubuntu.com/ubuntu/ precise-backports main restricted universe multiverse
deb-src http://cn.archive.ubuntu.com/ubuntu/ precise-backports main restricted universe multiverse

deb http://cn.archive.ubuntu.com/ubuntu/ precise-security main restricted
deb-src http://cn.archive.ubuntu.com/ubuntu/ precise-security main restricted
deb http://cn.archive.ubuntu.com/ubuntu/ precise-security universe
deb-src http://cn.archive.ubuntu.com/ubuntu/ precise-security universe
deb http://cn.archive.ubuntu.com/ubuntu/ precise-security multiverse
deb-src http://cn.archive.ubuntu.com/ubuntu/ precise-security multiverse

## Uncomment the following two lines to add software from Canonical's
## 'partner' repository.
## This software is not part of Ubuntu, but is offered by Canonical and the
## respective vendors as a service to Ubuntu users.
# deb http://archive.canonical.com/ubuntu precise partner
# deb-src http://archive.canonical.com/ubuntu precise partner

## This software is not part of Ubuntu, but is offered by third-party
## developers who want to ship their latest software.
## deb http://extras.ubuntu.com/ubuntu precise main
## deb-src http://extras.ubuntu.com/ubuntu precise main

#########################################################################################################

把中间的代码复制一份,重命名为sources.list然后覆盖源文件,之后在使用sudo apt-get updata命令,很快就可以更新完成,然后在安装  sudo apt-get install libglib2.0-dev 就会成功。
 
然后在检查 dpkg -l|grep glib应该就有了,


在编译下
gcc dello.c -o hello -lglib-2.0 通过
运行
 
./hello
nice!成功了! 
到目前为止仅仅是运行了标准输出hello,库里面的其他功能还有很远的路要走。
推荐一个牛人的入门网址
http://blog.chinaunix.net/uid-25696269-id-466217.html

以上是关于ubuntu完整安装glib心得的主要内容,如果未能解决你的问题,请参考以下文章

Glib 链接错误:未定义的引用

ubuntu12.04下编译时报错:dbus/dbus-glib.h:没有那个文件或目录,但是在/usr/include/dbus-1.0/dbus中有

安装 Sharp /usr/include/vips/vips8:35:25:致命错误:glib-object.h

ubuntu20.04 安装 elementaryOS 的 io.elementary.files 文件管理器

Glib C:程序无法识别 GLIB 库

管理心得