歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux綜合 >> 學習Linux >> Linux下glib源碼安裝使用方法

Linux下glib源碼安裝使用方法

日期:2017/3/6 9:31:38   编辑:學習Linux

Linux下glib源碼安裝使用方法


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

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

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

本文永久更新鏈接地址:

http://xxxxxx/Linuxjc/1141051.html TechArticle

Copyright © Linux教程網 All Rights Reserved