【问题标题】:using glib library in c program在 c 程序中使用 glib 库
【发布时间】:2012-07-13 08:34:30
【问题描述】:

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

【问题讨论】:

    标签: c linux glib


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      来自 IBM developerWorks 的 this pdf,如果您使用此命令标准安装了 glib,最好使用 pkg-config:

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

      您的包含看起来正确以及您使用它的方式。不确定' 是否会改变任何内容,但您可能想查看 PDF,其中包含大量示例和说明。

      【讨论】:

      • 感谢您的回答。我在 pdf 中这样做了,但无论如何我无法编译它。
      • 撇号应该是反引号。
      猜你喜欢
      • 2014-04-11
      • 1970-01-01
      • 1970-01-01
      • 2011-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-22
      相关资源
      最近更新 更多