简体   繁体   中英

using glib library in c program

I want to use hash tables in my c program.

I code:

...
#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);
...
}

Then I compile:

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

or the same command:

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

but anyway the result is:

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

Why I cant compile my program? I do wrong include of glib library?

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

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

From this pdf at IBM developper works, it's better to use the pkg-config if you have a standard install of glib with this command :

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

Your include look right and the way you are using it to. Not sure the ' will change anything but you might want to check the PDF, it contains a lot of examples and explainations.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM