Glib 是一種低階的函式庫,創建 GDK 和 GTK 應用程式時該函式庫可提供許多有用的定義和函式。
它全部是用C語言構成的,當要大量使用資料結構或演算法時,它真的是一個好的library,雖然不玩GTK,不過glib的功能可以簡化我的程式碼,不用寫的哩哩摳摳一大串,現在先從它的README看一下它的功能:
GLib Core Application Support
The Main Event Loop — manages all available sources of events
Threads — thread abstraction; including threads, different mutexes, conditions and thread private data
Thread Pools — pools of threads to execute work concurrently
Asynchronous Queues — asynchronous communication between threads
Dynamic Loading of Modules — portable method for dynamically loading 'plug-ins'
Memory Allocation — general memory-handling
IO Channels — portable support for using files, pipes and sockets
Error Reporting — a system for reporting errors
Message Output and Debugging Functions — functions to output messages and help debug applications
Message Logging — versatile support for logging messages with different levels of importance
GLib Utilities
String Utility Functions — various string-related functions
Character Set Conversion — convert strings between different character sets using iconv()
Unicode Manipulation — functions operating on Unicode characters and UTF-8 strings
Base64 Encoding — encodes and decodes data in Base64 format
Data Checksums — Computes the checksum for data
Internationalization — gettext support macros
Date and Time Functions — calendrical calculations and miscellaneous time stuff
Random Numbers — pseudo-random number generator
Hook Functions — support for manipulating lists of hook functions
Miscellaneous Utility Functions — a selection of portable utility functions
Lexical Scanner — a general purpose lexical scanner
Automatic String Completion — support for automatic completion using a group of target strings
Timers — keep track of elapsed time
Spawning Processes — process launching
File Utilities — various file-related functions
URI Functions — URI Functions
Hostname Utilities — Internet hostname utilities
Shell-related Utilities — shell-like commandline handling
Commandline option parser — parses commandline options
Glob-style pattern matching — matches strings against patterns containing '*' (wildcard) and '?' (joker)
Perl-compatible regular expressions — matches strings against regular expressions
Simple XML Subset Parser — parses a subset of XML
Key-value file parser — parses .ini-like config files
Bookmark file parser — parses files containing bookmarks
Testing — a test framework
Windows Compatibility Functions — UNIX emulation on Windows
GLib Data Types
Memory Slices — efficient way to allocate groups of equal-sized chunks of memory
Memory Chunks — deprecated way to allocate groups of equal-sized chunks of memory
Doubly-Linked Lists — linked lists containing integer values or pointers to data, with the ability to iterate over the list in both directions
Singly-Linked Lists — linked lists containing integer values or pointers to data, limited to iterating over the list in one direction
Double-ended Queues — double-ended queue data structure
Sequences — scalable lists
Trash Stacks — maintain a stack of unused allocated memory chunks
Hash Tables — associations between keys and values so that given a key the value can be found quickly
Strings — text buffers which grow automatically as text is added
String Chunks — efficient storage of groups of strings
Arrays — arrays of arbitrary elements which grow automatically as elements are added
Pointer Arrays — arrays of pointers to any type of data, which grow automatically as new elements are added
Byte Arrays — arrays of bytes, which grow automatically as elements are added
Balanced Binary Trees — a sorted collection of key/value pairs optimized for searching and traversing in order
N-ary Trees — trees of data with any number of branches
Quarks — a 2-way association between a string and a unique integer identifier
Keyed Data Lists — lists of data elements which are accessible by a string or GQuark identifier
Datasets — associate groups of data elements with particular memory locations
Relations and Tuples — tables of data which can be indexed on any number of fields
Caches — caches allow sharing of complex data structures to save resources
Memory Allocators — deprecated way to allocate chunks of memory for GList, GSList and GNode
從它文件中,可以看到它提供很多的功能,為了要了解它提供的一些功能,先把它移植到ARM,順便再做一些範例。 (PS:唉,又是ARM,誰叫我是做embedded的呢。不過其實一些功能已經在PC上測過了,真的還蠻方便的,不過一直沒實做到ARM上。)
CPU:ARM9
toolchain: GCC 4.2.1
glib dependence: libiconv , gettext
gettext-0.16.1.tar.gz
[root@ecken02 gettext-0.16.1]# ./configure --host=arm-linux --prefix=/usr/local/arm_linux_4.2/arm-linux
[root@ecken02 gettext-0.16.1]# make
[root@ecken02 gettext-0.16.1]# make install
libiconv-1.13.1.tar.gz
[root@ecken02 libiconv-1.13.1]# ./configure --host=arm-linux --prefix=/usr/local/arm_linux_4.2/arm-linux
[root@ecken02 libiconv-1.13.1]# make
[root@ecken02 libiconv-1.13.1]# make install
glib-2.20.5.tar.gz
[root@ecken02 glib-2.20.5]# ./configure --host=arm-linux --prefix=/usr/local/arm_linux_4.2/arm-linux/ --enable-static --disable-selinux --cache-file=arm-linux.cache
[root@ecken02 glib-2.20.5]# make
[root@ecken02 glib-2.20.5]# make install
編譯glib之前先設好一些參數,其原因
absurd在他的博客已有說明
#cat arm-linux.cache
ac_cv_type_long_long=yes
glib_cv_stack_grows=no
glib_cv_uscore=no
ac_cv_func_posix_getpwuid_r=yes
ac_cv_func_posix_getgrgid_r=yes
compile時會有一個錯誤,
giounix.c:185: error: 'SSIZE_MAX' undeclared
將#include <bits/posix1_lim.h> 加到giounix.c 即可
至此應該已經將glib library加入系統中了。接下來運用它提供的function來做些範例。