Search code examples
ccodeblocksgtk4

Compiler error "unkown type name 'GtkWidget" with Code::Blocks


I know the similar question to this, this ain't it; although my case is extremely similar. The first 4 lines of the Code::Blocks "Hello World" program when you open a new GTK project are this:

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

static void helloWorld (GtkWidget *wid, GtkWidget *win)

And the compiler build log goes as follows (with every instance of the type flagged, I only inlcuded the first for brevity):

-------------- Build: Debug in graphics project (compiler: GNU GCC Compiler)---------------

gcc -Wall -g -I/usr/include/gtk-4.0/ -I/usr/include/glib-2.0/ -I/usr/lib64/glib-2.0/include/ -I/usr/include/cairo/ -I/usr/include/pango-1.0/ -I/usr/include/harfbuzz/ -I/usr/include/gdk-pixbuf-2.0/ -c "/home/********/Documents/graphics project/main.c" -o obj/Debug/main.o
gcc  -o "bin/Debug/graphics project" obj/Debug/main.o    
/home/********/Documents/graphics project/main.c:4:25: error: unknown type name ‘GtkWidget’
    4 | static void helloWorld (GtkWidget *wid, GtkWidget *win)
      |                         ^~~~~~~~~

What?!? The GTK documentation states that 'GtkWidget' is the base class for darn near everything. I frankly don't know how this is possible. By way of explanation, the one thousand and one -I (as in India) directories here are because for some reason when I installed the GTK4 toolkit via command line, it put quite a few dependencies inside sub folders in the /usr/include/ directory and of course you can't tell the compiler to search all sub folders. The specific command I used to install GTK was

sudo yum install gtk4-devel

and I'm on Fedora 35 workstation running Code::Blocks version 20.03 and gcc-c++-11.2.1-9.fc.35.x86_64, if any of those things make a difference here. I even uninstalled GTK4 and reinstalled it, just to see if that would fix it--nope. I'm very new to programming in general and GUIs in particular, and I basically just wanted to screw around with the "Hello World" program without any specific goal in mind. I just want to learn. But this sort of thing has given me unending grief since minute one of trying to learn programming, and this time I can't Google the answer. Oh well, first time for everything.


Solution

  • I have been predominantly using Code Blocks for some time now on various GTK3 and GTK4 projects. I have also done some tweaking of the IDE as it pertains to building a GTK3 or GTK4 project, so in order to remember what a "vanilla" GTK project looked like with Code Blocks, I installed the IDE on a virtual Linux Mint machine and launched a new project. As with you, selecting "GTK" as my project type, the IDE generated a boilerplate "Hello World" program in the "main.c" file. However, I noticed one difference right away between the code generated on my machine and your example above. There is a difference in the "include" file set. Your set has the following:

    #include <stdlib.h>
    #include <gdk/gdk.h>
    
    static void helloWorld (GtkWidget *wid, GtkWidget *win)
    

    On my machine the first three lines of code are as follows:

    #include <stdlib.h>
    #include <gtk/gtk.h>
    
    static void helloWorld (GtkWidget *wid, GtkWidget *win)
    

    If indeed, your version of Code Blocks is referencing the GDK library (which contains information for objects such as cairo information and not the widget information), you may want to revise your reference to "include" file <gtk/gtk.h>. The other thing to note is that in the "vanilla" version of Code Blocks, the version of GTK that version 20.03 of Code Blocks references GTK2 libraries and configurations for the build process. If you happen to open the build information for the project, you are probably going to see a build setting such as the following under "Other Compiler Options":

    `pkg-config gtk+-2.0 --cflags`
    

    And, under linker options, you will probably find a library setting such as the following:

    `pkg-config gtk+-2.0 --libs`
    

    Due to that fact, your code probably won't compile if do not have the GTK2 libraries on your system. Linux Mint does have the GTK2 libraries installed by default. I do not know if Fedora does that.

    So for your initial "Hello World" project and subsequent projects, you could probably just revise the "include" file reference to <gtk/gtk.h> and see if the program will build.

    I hope that explanation clarifies things for you.

    Regards.