2012-07-13 90 views
3

我想在我的c程序中使用哈希表。在c程序中使用glib库

我代码:

... 
#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库吗?

回答

6

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

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

非常感谢! – Maxim 2012-07-13 09:01:00

1

this pdf在IBM developper的作品,它的更好,如果你有一个标准的与此命令安装的glib使用pkg配置:

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

你有看的权利,你正在使用它的方式至。不知道'会改变什么,但你可能想检查PDF,它包含了很多的例子和解释。

+0

谢谢你的答案。我在pdf中这样做,但无论如何,我不能编译这个。 – Maxim 2012-07-13 08:50:36

+0

撇号应该反引号。 – 2012-07-13 09:22:35