9

I have a sample C project that use GLib Library. In that source code, it use :

#include <glib.h>

When I compile, I found this error : "Glib.h : no such file or folder". I have google and find out that I should install this lib. So I use those command:

apt-get install libgtk2.0-dev
apt-get install glade

After that, I have checked and see already exist this header file in my system: usr/include/glib-2.0/glib.h But when I compile, I still meet problem above.

So I have change include line to :

#include <glib-2.0/glib.h>

So, after that, when I compile, I meet error inside glib.h header :

#ifndef __G_LIB_H__
#define __G_LIB_H__

#define __GLIB_H_INSIDE__

#include <glib/galloca.h>
#include <glib/garray.h>
// more code here

glib/galloca.h : no such file or directory. Because this error is inside system header file, I cannot modify anymore and still cannot compile.

I don't know how to fix this. I have read some post, that they change makefile. But, because my project is compiled automatically by IDE (CodeBlock) and I cannot really write a makefile, so that method doesn't suitable for me.

Please tell me a way to fix this.

Thanks :)

hqt
  • 29,632
  • 51
  • 171
  • 250
  • http://askubuntu.com/questions/58321/how-do-i-install-glib – berkay Feb 19 '14 at 19:41
  • @berkay this post as I have done. just install glib package. and i have installed it :( – hqt Feb 19 '14 at 19:48
  • Which IDE are you using? – hek2mgl Feb 19 '14 at 20:02
  • sorry :( I have added this option to my IDE (by add -lglib) but still meet that error :( should i put version behind that (which version ?) or should I do some other things for my IDE (Codeblocks) understand it ? Thanks :) – hqt Feb 19 '14 at 20:03
  • I must admit I did that once, but it's some days ago.. I would suggest: `lglib-2.0`. If this is not working, follow the glib hello world examples. This should enlight you. (as me once a day :) This one: https://developer.gnome.org/glib/2.37/glib-compiling.html – hek2mgl Feb 19 '14 at 20:07

2 Answers2

10

There must be some problem with how you build. To compile C programs that use GLib, you need package libglib2.0-dev. You can either install it directly, or install libgtk2.0-dev, which pulls it in as a dependency. So you have the packages you need.

The correct way to compile a GLib program is to use -I with the path to the GLib include files. An example (from How to compile a helloworld GLib program? on askubuntu):

gcc $(pkg-config --cflags --libs glib-2.0) hello_glib.c

This should let you compile this program:

#include <stdio.h>
#include <glib.h>
int main(int argc, char** argv) {
     GList* list = NULL;
     list = g_list_append(list, "Hello world!");
     printf("The first item is '%s'\n", g_list_first(list)->data);
     return 0;
}

The errors you are getting indicate that you are not setting the include path (-I) correctly. How to do this depends on your build system/IDE.


In Code::Blocks, you must set the include path and the linker options in the appropriate configuration dialog. Run pkg-config --cflags --libs glib-2.0, which will output something like

-I/usr/include/glib-2.0 -I/usr/lib/i386-linux-gnu/glib-2.0/include  -lglib-2.0  

The directories after -I must be set in the compiler options of your project (should be under Project -> Build Options -> Search Directories), and the names after -l must be set in the linker settings. Another option is to create a Makefile, and let Code::Blocks use that.

See e.g. Q: What do I need to know when using 3rd party libs? in the Code::Blocks Wiki.

Community
  • 1
  • 1
sleske
  • 81,358
  • 34
  • 189
  • 227
  • Poor answer for a person who was voting everybody down here.. A link to the ubuntu help page would have been enough, in comments of course. It remains the open question: `How to do this depends on your build system/IDE.` Good job! – hek2mgl Feb 19 '14 at 20:02
  • @hek2mgl: Sorry if I ruined your day. I added some information about how to solve this in Code::Blocks. And I only voted you answer down because it proposed something that clearly coud not work. Anyway, now your answer is deleted, my downvote no longer counts against your rep (AFAIK). Also, feel free to add a better answer which gets more votes than mine :-). – sleske Feb 19 '14 at 21:12
  • No worry, my day is safe. :) I was about to click the delete button just in the moment you dropped that comment because I've used `apt-cache show libgkt2.0-dev`. Ok, I should have done this *before* attempting to answer the question. But imo a "hey you missed that ... " comment would have been enough... – hek2mgl Feb 19 '14 at 21:21
  • in linker setting. I press add and add two directories after running above command (so appear in two line). about -lglib-2.0, does we need anything with this parameter ? thanks :) – hqt Feb 20 '14 at 02:13
2

You should not alter your source code (e.g. the #include directives).

You just need to use pkg-config (both for compiling, with --cflags, and for linking, with --libs), preferably with a builder program like make.

This is an example for exactly your situation: a Makefile using pkg-config to compile some source program using glib

Community
  • 1
  • 1
Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547