歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux技術 >> ubuntu完整安裝glib心得

ubuntu完整安裝glib心得

日期:2017/3/3 13:03:06   编辑:Linux技術
學習完數據結構,感覺對數據的存儲有了一種新的認識,在動態內存的基礎上,鏈表有了絕對的靈活性,可以給未知長度的數據處理帶來方便,但是在不停的寫代碼的過程中,感覺到了深深的仇恨,每次要實現功能,幾乎源代碼都有改動,也就是說每個使用鏈表的方法幾乎都不一樣,這就對自己編寫的鏈表、隊列、和棧的函數維護成本增加,甚至N天以後不知道每個版本的實際差別。
這是我就在想是不是linux能夠提供現成的鏈表實現呢,畢竟在內核中大量的實現鏈表的神乎其神的源代碼,會不會好心的封裝成庫給其他人使用呢?很可惜linux沒有做,所以基本上要使用鏈表以及隊列都需要自己去實現源碼,對於我這樣一個懶癌重度患者這是不能忍受的,我就在網上找相關的資料,終於讓我發現了第三方glib庫。這個庫的功能很強大,從小的g_uint8類型的定義,到g_thread線程的實現,這個庫都可做到,下面簡單介紹一下,完整的功能說明自己去百度吧。
glib介紹
glib庫是Linux平台下最常用的C語言函數庫,它具有很好的可移植性和實用性。
1 類型定義
整數類型. 布爾類型. 字符型. 浮點型.
指針. gconstpointer對於於標准C的const void*
2 glib宏
常用宏
整型與指針類型間的轉換
調試宏
前提條件檢查,斷言, 判斷構建是否是指定的構件
3 常用函數
4 內存管理
4 字符串處理
字符串操作, 修改字符串, 字符串轉換, 其他字符串轉換函數
5 數據結構
鏈表, 樹, 哈希表,
6 GString
GString類似於標准C的字符串類型,但是GString能夠自動增長,這些特性可以防止程序中的緩沖區溢出。下面是GString的定義:
7.計時器函數
8 錯誤處理函數
9 其它實用函數
基本上能夠用的上的功能這個庫都有實現
好了下面進入正題,一起來對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
Copyright © Linux教程網 All Rights Reserved