3

Hi everyone I have a program with the following includes:

  • gtk/gtk.h
  • glib.h

I have used the commands:

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

But I am still getting the error that glib was not found and gtk/gtk.h was not found. It's the first time I am using gtk and I have no idea how it works or how to install it.

trxgnyp1
  • 317
  • 1
  • 10
tariq
  • 529
  • 5
  • 19
  • 34

3 Answers3

11

The command you're supposed to use (in more recent releases of linux/gtk) is pkg-config, not gtk-config. gtk-config is intended for pre 2.0 gtk development.

Consider the file you're compiling is called foo.c, to compile it under gtk-2.0, you would use, from the command line the command:

gcc `pkg-config --cflags glib-2.0 gtk+-2.0` foo.c -o foo `pkg-config --libs glib-2.0 gtk+-2.0`

This should compile, and give you a file foo, that can be executed.

but really, use a makefile, as this stuff is a pain to keep typing. I would write out a sample makefile, but there are rules that need to be followed in the formatting of them that makes it difficult to type in the editor window.

# Sample Makefile
CFLAGS := $(shell pkg-config --cflags glib-2.0 gtk+-2.0)
LDFLAGS := $(shell pkg-config --libs glib-2.0 gtk+-2.0)

foo: foo.c
<TAB HERE NOT SPACES>$(CC) $(CFLAGS) $< -o $@ $(LDFLAGS)

This defines a simple rule saying to make foo, it depends on foo.c, so of foo.c is newer than foo, it will be rebuilt. Where I write 'TAB HERE NOT SPACES' it must be a tab character, and cannot be a set of space characters.

Anya Shenanigans
  • 91,618
  • 3
  • 107
  • 122
  • thanks @Petesh , i was looking for the fix for this the whole day , it turned out that the order in which we compile does matter, for an instance .. when I was using $ gcc $(pkg-config --cflags --libs glib-2.0) file.c it wasn't working properly , still giving me error for ''undefined reference to 'g_list_append' and 'g_list_first'" but compiling it in '' gcc $(pkg-config --cflags glib-2.0) practice.c -o practice $(pkg-config --libs glib-2.0)" solved my problem :) – lazarus Sep 05 '18 at 09:17
  • 1
    You can move the entire `$(pkg-config --cflags --libs glib-2.0)` to the end of the compilation line also if you just want the single invocation of pkg-config. The stuff in `--cflags` works regardless of where it is on the command line, the `--libs` piece needs to be after the reference to `practice.c` in order to have it work. – Anya Shenanigans Sep 05 '18 at 09:21
  • thanks @Petesh, It worked as you mentioned. ''''anupam:GLIBC$ gcc practice.c -o practice $(pkg-config --cflags --libs glib-2.0) --> this worked --> anupam:GLIBC$ gcc $(pkg-config --cflags --libs glib-2.0) practice.c -o practice /tmp/ccgX9tV2.o: In function `main': practice.c:(.text+0x26): undefined reference to `g_list_append' practice.c:(.text+0x36): undefined reference to `g_list_first' collect2: error: ld returned 1 exit status --> this failed '''' – lazarus Sep 05 '18 at 09:28
2

type "locate glib.h" to determine file's location (assuming a contemporary linux distribution - your post doesn't provide much information).

Then ensure the path to glib.h is properly specified in your Makefile. (You do have a Makefile, don't you?) Perform the same steps for gtk.h.

Throwback1986
  • 5,887
  • 1
  • 30
  • 22
1

Please read the official documentation. It explains how to compile GTK applications. Basically to compile a hello.c file to generate a hello program, you'll type:

gcc `pkg-config --cflags --libs gtk+-2.0` hello.c -o hello
liberforce
  • 11,189
  • 37
  • 48