Linux下glib源碼安裝使用方法

來源:互聯網
上載者:User

Linux下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解壓

解壓後進入目錄後,三部曲:

./configuremakemake 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,解壓、進目錄,三部曲:

./configuremakemake install

3、libffi問題

報錯如下:

No package 'libffi' foundConsider adjusting the PKG_CONFIG_PATH environment variable if youinstalled software in a non-standard prefix.Alternatively, you may set the environment variables LIBFFI_CFLAGSand 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,解壓、進目錄,三部曲:

./configuremakemake 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,解壓、進目錄,三部曲:

./configuremakemake 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' foundConsider adjusting the PKG_CONFIG_PATH environment variable if youinstalled software in a non-standard prefix.Alternatively, you may set the environment variables PCRE_CFLAGSand 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相容的Regex庫。這些在執行正規運算式模式比對時用與Perl 5同樣的文法和語義是很有用的,也可以來解決C語言中使用Regex的問題。

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

我下載了pcre-8.38.tar.gz,解壓、進目錄,改動的三部曲:

./configure --enable-utf8 --enable-unicode-propertiesmakemake install

如果第一步只是./configure,到時候安裝glib會繼續報錯:

checking for Unicode support in PCRE... noconfigure: 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-23Compiled 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 directorypcretest: 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 /libln -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)

本文永久更新連結地址:

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.