在c程序中使用glib库

我想在我的c程序中使用散列表。

我编码:

... #include <glib.h> void main(int argc, char **argv) { GHashTable *g_hash_table; ... g_hash_table = g_hash_table_new(g_int_hash, g_int_equal); ... } 

然后我编译:

 $ gcc -I/usr/include/glib-2.0 -I/usr/lib/i386-linux-gnu/glib-2.0/include -lglib-2.0 -o test test.c 

或相同的命令:

 $ gcc `pkg-config --cflags --libs glib-2.0` -o test test.c 

但无论如何结果是:

 test.c: underfined reference to `g_int_equal` test.c: underfined reference to `g_int_hash` test.c: underfined reference to `g_hash_table_new` collect2: ld returned 1 exit status 

为什么我不能编译我的程序? 我做错了包括glib库?

您需要在使用它们的源文件和目标文件之后在命令行中指定库:

 gcc test.c `pkg-config --cflags --libs glib-2.0` -o test 

在IBM开发者的工作中,如果使用这个命令标准安装glib ,最好使用pkg-config:

 $ gcc `pkg-config --cflags --libs glib-2.0` -o ex-compile ex-compile.c 

你的包括看起来正确,你用它的方式。 不确定'会改变什么,但你可能想检查PDF,它包含了很多的例子和解释。