3

我想在我的 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 库?

4

2 回答 2

7

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

gcc test.c `pkg-config --cflags --libs glib-2.0` -o test 
于 2012-07-13T08:57:57.897 回答
1

从IBM developerper 的这个 pdfglib文件中,如果您使用以下命令进行标准安装,最好使用 pkg-config :

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

您的包含看起来正确以及您使用它的方式。不确定'会改变什么,但您可能想查看 PDF,它包含很多示例和说明。

于 2012-07-13T08:44:38.460 回答