下載最新的glib.2.28.2
./configure --prefix=/usr
make clean && make && make install
(1)遇到問題
make的時候 缺少 magic.h標頭檔;這個標頭檔在centos的/usr/include/linux/裡確實沒有,我把http://tomoyo.sourceforge.jp/cgi-bin/lxr/source/include/linux/magic.h這個,直接拷貝到/usr/include/linux/下,順利通過;
(2)make的時候出現這個錯誤: http://blog.csdn.net/saint1126/archive/2011/01/17/6147169.aspx
make[4]: Entering directory `/root/Desktop/glib-2.26.0/gio/tests'
/usr/bin/msgfmt -o test.mo ./de.po; /
/bin/mkdir -p de/LC_MESSAGES; /
cp -f test.mo de/LC_MESSAGES
./de.po:15: 關鍵字“msgctxt”未知
./de.po:15:8: parse error
/usr/bin/msgfmt: 發現 2 處致命錯誤
原因:需要升級gettext package的庫,操作如下:
- wget http://ftp.gnu.org/pub/gnu/gettext/gettext-0.18.1.1.tar.gz
- tar xvzf gettext-0.18.1.1.tar.gz
- cd gettext-0.18.1.1
- ./configure
- make
- make install
- ldconfig
這裡在網上隨便找了一個test:
/* until.c 用來測試實用功能 */
#include <glib.h>
int main(int argc, char *argv[])
{
GRand *rand;
GTimer *timer;
gint n;
gint i, j;
gint x = 0;
rand = g_rand_new(); //建立隨機數對象
for(n=0; n<20; n++)
{ //產生隨機數並顯示出來
g_print("%d/t",g_rand_int_range(rand,1,100));
}
g_print("/n");
g_rand_free(rand); //釋放隨機數對象
//建立計時器
timer = g_timer_new();
g_timer_start(timer);//開始計時
for(i=0; i<10000; i++)
for(j=0; j<3000; j++)
x++;//累計
g_timer_stop(timer);//計時結束
//輸出計時結果
g_print("%ld/tall:%.2f seconds was used!/n",x,g_timer_elapsed(timer,NULL));
}
使用這個編譯: gcc -g `pkg-config --cflags --libs glib-2.0` t_glib.c -o t_glib