Unable to install gtk2

I am learning to program in C and I need to have gtk2 installed in order to access the gtk library. When I run the command sudo zypper search gtk the output shows that the gtk2-tools is installed. I am currently running OpenSUSE 15.1 with a KDE desktop environment. Thank you in advance for your assistance.

Hi
If you use a wildcard for searching, plus if programming, then your after the development package, gtk2-devel to be installed.


zypper se gtk*devel

You should not need to use sudo for searches and also only develop as your user, become root to install :wink:

malcolmlewis,
Thank you for your assistance. That is what I was looking for to fix my problem. I also appreciate your guidance on the use of sudo.

Now that I have the gtk library installed I am having trouble compiling my code. In OpenSUSE the gtk.h file is located at /usr/include/gtk-2.0/gtk/gtk.h. I have added this location to my code but I an still getting the following error.

In file included from chapter14.c:1:0:
/usr/include/gtk-2.0/gtk/gtk.h:32:21: fatal error: gdk/gdk.h: No such file or directory
compilation terminated.

How do I fix this issue?

Hi
In your code you should just have the following?


#include <gtk-2.0/gtk/gtk.h>
#include <gtk-2.0/gtk/gdk.h>

Both headers and in the gtk directory, can you confirm?

Check permissions.

malcolmlewis,
The headers are in different directories. Under the gtk-2.0 directory there is a gdk directory and a gtk directory. Each header is located in the respective directory.

What should the permissions be for each file?

Hi
So what does your code look like?

Normally the makefile should automatically add the /usr/include, so it would be;


#include <gtk-2.0/gtk/gtk.h>
#include <gtk-2.0/gdk/gdk.h>

I am following a a guide from Raspberry Pi, *C GUI Programming. *I do not have a make file. The guide explains that I should be able to compile my code. My code is shown below.


#include <gtk-2.0/gtk/gtk.h>
#include <gtk-2.0/gdk/gdk.h>

int main (int argc, char *argv])
{
        gtk_init (&argc, &argv);
        GtkWidget *win = gtk_window_new (GTK_WINDOW_TOPLEVEL);
        gtk_widget_show (win);
        gtk_main();
        return 0;
}

Hi
If you run;


pkg-config --cflags --libs gtk+-2.0

I suspect the gtk-2.0 is already in the include directory, so remove that from the #include line


#include <gtk/gtk.h>
#include <gdk/gdk.h>

You are correct, the gtk-2.0 directory is in the include directory. I ran the pkg-config command that you recommended, but I still received the error. It appears that the directory tree that each header file is looking for is different than their actual location. I started editing each header file as an error appeared by adding the parent directory but for each one that I fix another error shows up. There has to be a better way to fix this problem.

Hi
Welcome to the world of cross-distribution coding… :wink: You can use the likes of find and sed to go through and make the changes globally.

Great, I must be sick or demented but I still enjoy it even if I do end up pulling my hair out. :beat-up: I am not familiar with the use of sed. I will do some research and see what I can find.

Hi
So is it just those particular headers or other ones as well?

Create a test folder to work on and use something like;


find ./ -type f -exec sed -i 's/\/gtk.h/\/gtk\/gtk.h/g' {} \;

After some work I have managed to compile my code. By adding using the following command, which you recommended before, but I added it to the end of my gcc command as shown below. I had tried this before but I was using the wrong punctuation around the pkg-config. Thank you for all of your assistance. :slight_smile:

gcc -o myprog14 chapter14.c `pkg-config --cflags --libs gtk+-2.0`

Hi
Good job :slight_smile: But yes those sorts of things are taken care of by the likes of a Makefile and configure (autotools) scripts… :wink: