Your hello-world program fails to build in XCode

I tried your hello world program in XCode and it failed to build.
I got a “Build failed” message, but no errors were flagged in the source. Here is my code:

#include </opt/local/include/gtk-3.0/gtk/gtk.h>

static void print_hello (GtkWidget *widget, gpointer data) { g_print (“Hello World\n”); }

static void activate (GtkApplication *app, gpointer user_data) { GtkWidget *window; GtkWidget *button;

window = gtk_application_window_new (app); gtk_window_set_title (GTK_WINDOW (window), “Window”); gtk_window_set_default_size (GTK_WINDOW (window), 200, 200);

button = gtk_button_new_with_label (“Hello World”); g_signal_connect (button, “clicked”, G_CALLBACK (print_hello), NULL); gtk_window_set_child (GTK_WINDOW (window), button);

gtk_window_present (GTK_WINDOW (window)); }

int main (int argc, char **argv) { GtkApplication *app; int status;

app = gtk_application_new (“org.gtk.example”, G_APPLICATION_FLAGS_NONE); g_signal_connect (app, “activate”, G_CALLBACK (activate), NULL); status = g_application_run (G_APPLICATION (app), argc, argv); g_object_unref (app);

return status; }

Please advise how I should continue.

I’ve discovered the problem. Your file gtk.h references gdk.h, but does not include its full path.
When I try to edit gtk.h to include the full path to gdk.h, I’m told the file is locked and cannot be edited.

The path to my gtk.h is </opt/local/include/gtk-3.0/gtk/gtk.h>

How can I solve this problem?

To anticipate your next question, my gtk files are at that location on my HD because that’s where MacPorts installed them. I’ve tried moving them elsewhere but run into the same problem trying to build with XCode.

Wow, I’ve never seen anybody attempt to use absolute paths in include statements before. This is not advisable because nobody else will be able to compile your program unless they have everything installed in exactly the same place as you. The only correct way to include gtk.h is #include <gtk/gtk.h>. If that doesn’t work, then your build system is broken and you need to fix your include flags to point to the correct paths before proceeding further. The best way to do this is to use pkg-config, though I doubt XCode supports that: you might have to configure it manually. No clue how to do that in XCode.

1 Like

As @mcatanzaro says, please use #include <gtk/gtk.h>, and use pkg-config to determine the location where GTK and friends got installed.

You may find this useful for Xcode.

Thanks for the response. Exactly how do I use pkg-config to detect gtk?

The first few paragraphs of https://docs.gtk.org/gtk3/compiling.html explain what pkg-config does and how to use it in a build command.

When I copy and paste the following into terminal:

$ pkg-config --cflags gtk±3.0
-pthread -I/usr/include/gtk-3.0 -I/usr/lib64/gtk-3.0/include -I/usr/include/atk-1.0 -I/usr/include/cairo -I/usr/include/pango-1.0 -I/usr/include/glib-2.0 -I/usr/lib64/glib-2.0/include -I/usr/include/pixman-1 -I/usr/include/freetype2 -I/usr/include/libpng12
$ pkg-config --libs gtk±3.0
-pthread -lgtk-3 -lgdk-3 -latk-1.0 -lgio-2.0 -lpangoft2-1.0 -lgdk_pixbuf-2.0 -lpangocairo-1.0 -lcairo -lpango-1.0 -lfreetype -lfontconfig -lgobject-2.0 -lgmodule-2.0 -lgthread-2.0 -lrt -lglib-2.0

'I get:
zsh: command not found: $
zsh: command not found: -pthread

I guess I still need more help.

The ‘$’ is not part of the command, it represents the prompt in the terminal where you enter the command. The line after the ‘$’ is not part of the command, it shows the response echoed back in the terminal. So the command to enter is the text following the ‘$’ up to the end of the line. (The copy code to clipboard button is not useful here because this isn’t code - it is a mixture of terminal input and output.)

I deleted the MacPorts install of GTK.
Then I ran the installation script gtk-osx-build-setup.sh
Then I ran:

pkg-config --cflags gtk+-3.0
 -pthread -I/usr/include/gtk-3.0 -I/usr/lib64/gtk-3.0/include -I/usr/include/atk-1.0 -I/usr/include/cairo -I/usr/include/pango-1.0 -I/usr/include/glib-2.0 -I/usr/lib64/glib-2.0/include -I/usr/include/pixman-1 -I/usr/include/freetype2 -I/usr/include/libpng12

according to the instructions for Compiling GTK Applications on UNIX, and I got:
zsh: command not found: -pthread

-pthread is a compiler flag, not a command. You can’t run it on its own separate line.

OK, I ran:

pkg-config --cflags gtk+-3.0

which completed without error.
Then I followed the instruction to run
cc pkg-config --cflags gtk+-3.0 hello-gtk.c -o hello-gtk pkg-config --libs gtk+-3.0
to compile my hello-gtk.c file and I got:

hello-gtk.c:1:10: fatal error: ‘gtk/gtk.h’ file not found
#include <gtk/gtk.h>
^~~~~~~~~~~
1 error generated.

Apparently, I’m still missing something.

Hey, it looks to me like you should have executed

pkg-config --cflags gtk+-3.0 -pthread -I/usr/include/gtk-3.0 -I/usr/lib64/gtk-3.0/include -I/usr/include/atk-1.0 -I/usr/include/cairo -I/usr/include/pango-1.0 -I/usr/include/glib-2.0 -I/usr/lib64/glib-2.0/include -I/usr/include/pixman-1 -I/usr/include/freetype2 -I/usr/include/libpng12

all in one line instead of omitting the rest of the command.

No, that’d be definitely wrong. The second line is the output of pkg-config.

The “backtick command backtick” syntax means: “execute command, and then put the output in the current command line”; I don’t know what kind of shell you’re using, so maybe that doesn’t work for you.

My suggestion is to run these two commands:

export GTK_CFLAGS="$( pkg-config --cflags gtk+-3.0 )"
export GTK_LIBS="$( pkg-config --libs gtk+-3.0 )"

and then use:

cc $GTK_CFLAGS hello-gtk.c -o hello-gtk $GTK_LIBS

I also recommend reading some documentation on how to use the C compiler from the command line.

In any case, this is going to be useful up until the point when you need to start writing code with GTK; once you reach that point, using a build system like Meson will remove a lot of the complications and pain.

I don’t think the backticks were missing. It looks like Discourse treated them as markdown (a code block wasn’t used).

@dgpdx Now that you have deleted the MacPorts install of GTK, where is your gtk/gtk.h? To help you, please report the output of pkg-config --cflags gtk+-3.0 and the location of your gtk/gtk.h file.

I thought running the installation shell script was supposed to install gtk.
Is that not true?

I did all these things and still got the same error: <gtk/gtk.h> not found.

Thanks for the tip about Meson, I will look into using it.

What does:

pkg-config --modversion gtk+-3.0

print out?

pkg-config --modversion gtk±3.0 prints 3.24.31