Author Topic: Link a library in Codeblocks problem  (Read 5851 times)

Offline dali1985

  • Single posting newcomer
  • *
  • Posts: 5
Link a library in Codeblocks problem
« on: July 02, 2013, 01:50:03 pm »
I would like to parse a config file using glib in Codeblocks which I use. So I want to do exactly the example which is described http://gtkbook.com/tutorial.php?page=keyfile first. I have a file named myconfig.cfg and and a code programming.c. I just copy and paste the code to see if glib works but unfortunately it does not work. I did the installation of glib2.0 using sudo apt-get, I found where are the libs in glibs using

pkg-config --cflags --libs glib-2.0
and in this path

project->Build Options->Compiler Settings-> Other Options
I added

-I/usr/include/glib-2.0 -I/usr/lib/arm-linuxgnuebihf/glib-2.0/include
When I build and run the programming.c I have these errors

Code
-------------- Build: Debug in programming ---------------
gcc -Wall  -g -I/usr/include/glib-2.0 -I/usr/lib/arm-linux-gnueabihf/glib-2.0/include  -std=c99    -c /home/pi/Desktop/programming/main.c -o obj/Debug/main.o
g++  -o bin/Debug/programming obj/Debug/main.o    /usr/lib/libmysqlclient.so.16
obj/Debug/main.o: In function `main':
/home/pi/Desktop/programming/main.c:22: undefined reference to `g_key_file_new'
/home/pi/Desktop/programming/main.c:26: undefined reference to `g_key_file_load_from_file'
/home/pi/Desktop/programming/main.c:28: undefined reference to `g_log'
/home/pi/Desktop/programming/main.c:34: undefined reference to `g_slice_alloc'
/home/pi/Desktop/programming/main.c:37: undefined reference to `g_key_file_get_string'
/home/pi/Desktop/programming/main.c:39: undefined reference to `g_key_file_get_locale_string'

/home/pi/Desktop/programming/main.c:41: undefined reference to `g_key_file_get_boolean_list'
/home/pi/Desktop/programming/main.c:43: undefined reference to `g_key_file_get_integer_list'
/home/pi/Desktop/programming/main.c:45: undefined reference to `g_key_file_get_string_list'
/home/pi/Desktop/programming/main.c:47: undefined reference to `g_key_file_get_integer'
/home/pi/Desktop/programming/main.c:49: undefined reference to `g_key_file_get_double_list'
collect2: ld returned 1 exit status
Process terminated with status 1 (0 minutes, 6 seconds)
11 errors, 0 warnings

Am I missing something? I tried also to do in the same way with libconfig but again I have undefined reference. Is the problem the path?

UPDATE

With the usage of pkg-config --libs glib-2.0 it returns me -lglib-2.0

In Codeblocks I inserted it(lglib-2.0) in

project->Build Options->Linker Settings-> Link Libraries
and now I have this error:
Code
**(process:3751): ERROR ** No such file or directory  
Trace/breakpoint trap
« Last Edit: July 02, 2013, 01:51:34 pm by dali1985 »

Offline Jenna

  • Administrator
  • Lives here!
  • *****
  • Posts: 7252
Re: Link a library in Codeblocks problem
« Reply #1 on: July 02, 2013, 02:04:09 pm »
You don't need the "l" in front of the glib2.0 here.

But the better way is to use pkg-config directly, so changes in the libraries layout will be recognized automatically.

Just add
Code
`pkg-config --cflags glib-2.0`
to the the projects build options in "Compiler settings -> Other options", and
Code
`pkg-config --libs glib-2.0`
in "Linker settings -> Other linker options".

Note the backticks around the commands.