cmacosincludegtk3macports

How to compile code with gtk.h in C under MacOS?


I have 40 years of computer experience but I'm self taught and I'm intermediate at the C language so I'm not going to know more advanced topics. I'm trying to learn to use GTK+ by watching these YouTube videos but they're for Linux. I want my programs to be cross-platform to Windows and Mac. I have a 2019, Intel, Macbook Pro with Ventura (MacOS 13.4.1). I haven't been able to find a tutorial on how to install the GTK+ header files on Ventura. The best I could do was to use MacPorts. MacPorts put gtk.h into "/opt/local/include/gtk-3.0/gtk/gtk.h". I used this code:

#include <gtk/gtk.h>

int main(int argc, char **argv)
{

gtk_init (&argc, &argv);

// gtk codes goes here

gtk_main ();

return 0;
}

and this compile instruction:

gcc `pkg-config --cflags gtk+-3.0` -o start start.c `pkg-config --libs gtk+-3.0`

but got this error:

zsh: command not found: pkg-config 
zsh: command not found: pkg-config 
In file included from start.c:1: /opt/local/include/gtk-3.0/gtk/gtk.h:30:10: fatal error: 'gdk/gdk.h' file not found
#include <gdk/gdk.h>
         ^~~~~~~~~~~ 
1 error generated.

Changing the include directive to #include "/opt/local/include/gtk-3.0/gtk/gtk.h" gave me the same error. What do I need to do to get the code to compile?


Solution

  • The compilation command you are using depends on the pkg-config command, which MacOS does not provide by default. You should be able to install it from MacPorts.

    As you already discovered, Gtk is X11-based, so you will also need an X server. MacOS has not provided an X server for years, but Apple did spin off the one it once provided into a separate project, XQuartz, which is still maintained.