Your hello-world program fails to build in XCode

Mostly because very few people actually use macOS, XCode, and GTK.

The few that do are mostly working on Linux apps and porting them to macOS, which means they know how to use a toolchain, and they don’t use XCode at all. Various free and open source applications use jhbuild on macOS; but, again: those are actual applications, typically developed on other platforms.

GTK on macOS builds every dependency it can, and can be used as a Meson subproject for your own project; of course, that assumes you’re actually writing a project. It does not work if you’re trying to compile a single file “Hello World” example.

I have a command line C app that cries out for a GUI. Is gtk the best resource for that, or is there something easier to use?

I’ve decided to abandon gtk as a realistic solution to my GUI needs. I wish to thank all those who took the time and effort to assist me in this forum! You are a valuable resource!

I will eventually port my app to Java which has built-in GUI capabilities.

1 Like

Hope you have more luck with other tools!

Many thanks for your assistance!

I think you’ve snatched defeat from the jaws of victory here :slight_smile: From the discussion above, it looks like you removed the MacPorts installation just before using the correct build command with the correct #include in your source.

You are learning some new concepts at the same time. As you weren’t familiar with using a terminal, communication has been harder and documentation assumes familiarity with using a terminal.

It is recommended that a beginner installs a package on their system using a package manager. You did that initially with MacPorts (you could have used Brew). Unfortunately you went ‘upstream’ to GTK, using If you go upstream to the source projects, you have to build the project yourself and there is usually more complexity. In this case, the jhbuild setup was likely not providing you with the environment you expected. I would recommend beginners use package managers if possible.

Also, some general points to help you diagnose issues in other forums if not here. All should be taken constructively.

Don’t assume what is wrong, state what you were trying to achieve. For example you assumed that MacPorts didn’t install gtk.h and most people would think they couldn’t help you. However, MacPorts had installed it and others could have helped as that wasn’t the issue.

If you quote the following in your message:

  1. source file
  2. the command you used to build it
  3. the error message you received

then most people here would likely be able to quickly point out what is wrong. You should include these verbatim. In this discourse forum that is achieved by putting the contents inside triple backtick markers e.g.

cc `pkg-config --cflags gtk+-3.0` hello.c -o hello `pkg-config --libs gtk+-3.0`

so this markdown renders as

cc `pkg-config --cflags gtk+-3.0` hello.c -o hello `pkg-config --libs gtk+-3.0`

I hope that helps. Good luck in future.

Another problem was the title of the topic referencing “your” hello world program. It’s quite unlikely that anybody involved with GNOME would write a hello world that begins with #include </opt/local/include/gtk-3.0/gtk/gtk.h>. Surely that’s really your own…

I only did that after <gtk/gtk.> was not found. I’ve since changed it back.

Ok, I will try this one more time following your directions. Thanks!

I installed gtk using brew. Then I tried your suggestion and git this:

don@MacBook-Pro GTKACL % pkg-config --cflags gtk+-3.0 hello-gtk.c -o hello-gtk pkg-config --libs gtk+-3.0

zsh: no such file or directory: -I/opt/local/include/gtk-3.0

You’ve missed off the ccat the start of the command. It should be

cc `pkg-config --cflags gtk+-3.0` hello-gtk.c -o hello-gtk `pkg-config --libs gtk+-3.0`


zsh: no such file or directory: -I/opt/local/include/gtk-3.0

/opt/local/... is typically the path used by MacPorts so it looks like MacPorts is still there. Did you entirely uninstall MacPorts before installing Brew or are you aware how to use both on your system? If so, that’s fine. If not, you must use either MacPorts or Brew. Do not mix them as it won’t work. (By all means use Brew but I won’t be able to help with any Brew issues as I don’t use it.)

I deleted the /opt/local/include/gtk-3.0 directory.

When I just ran cc pkg-config --cflags gtk+-3.0 hello-gtk.c -o hello-gtk `pkg-config --libs g

I got this:
cc pkg-config --cflags gtk+-3.0 hello-gtk.c -o hello-gtk pkg-config --libs gtk+-3.0

Package gtk±3.0 was not found in the pkg-config search path.

Perhaps you should add the directory containing `gtk±3.0.pc’

to the PKG_CONFIG_PATH environment variable

No package ‘gtk±3.0’ found

Package gtk±3.0 was not found in the pkg-config search path.

Perhaps you should add the directory containing `gtk±3.0.pc’

to the PKG_CONFIG_PATH environment variable

No package ‘gtk±3.0’ found

2022-04-22 03:23:38.224 xcodebuild[8753:109784] Requested but did not find extension point with identifier Xcode.IDEKit.ExtensionSentinelHostApplications for extension Xcode.DebuggerFoundation.AppExtensionHosts.watchOS of plug-in

2022-04-22 03:23:38.225 xcodebuild[8753:109784] Requested but did not find extension point with identifier Xcode.IDEKit.ExtensionPointIdentifierToBundleIdentifier for extension Xcode.DebuggerFoundation.AppExtensionToBundleIdentifierMap.watchOS of plug-in

hello-gtk.c:1:10: fatal error: ‘gtk/gtk.h’ file not found

#include <gtk/gtk.h>


1 error generated.

Why? The directory hierarchy under /opt/local is managed by MacPorts and you must never make manual changes there. All changes should be done via the port command. I don’t know what state your machine is in now. Until you remove Brew and fix your MacPorts environment (ask on a MacPorts forum - I’m not sure how) I don’t think I can help you. Once you do that, your command

cc `pkg-config --cflags gtk+-3.0` hello-gtk.c -o hello-gtk `pkg-config --libs gtk+-3.0`

should compile your code and you’ll get the complier output.

I uninstalled brew, re-installed gtk using MacPots and it appears now have gtk installed correctly!

Attempting to compile hello-gtk.c, I now get:

hello-gtk.c:23:3: error: implicit declaration of function ‘gtk_window_set_child’ is invalid in C99 [-Werror,-Wimplicit-function-declaration]
gtk_window_set_child (GTK_WINDOW (window), button);
1 error generated.

This is something I can work with. Many thanks!

You are using the GTK4 “hello world” example with GTK3.

Ah. I copied the hello.c program given as an example. Thanks for the tip. How do I install gtk4?

MaPorts does not have gtk4.

You can continue to use GTK 3 for learning with the GTK 3 tutorial. There is a hello world example in there. Most of what you learn would transfer to GTK 4.

For GTK 4, I believe you will need to use Brew instead of MacPorts. Sorry, I didn’t see that you had the GTK 4 example there. I’ve asked about the status of the MacPorts gtk4 port.

It compiles ok, but when I run it, I get:

don@MacBook-Pro GTKACL % gcc pkg-config --cflags gtk+-3.0 -o example-0 example-0.c pkg-config --libs gtk+-3.0

don@MacBook-Pro GTKACL % ./example-0
(example-0:24896): Gtk-WARNING **: 16:34:18.169: cannot open display:

don@MacBook-Pro GTKACL %

I would suggest doing a Google search for ‘macports < your error message >’ as these questions have probably been asked many times before. Do you have X11 available? For example, try sudo port install xorg-server.

If you get an error about D-Bus, see this comment.

It seems macports is still shipping GTK3 with X11 enabled, which is wrong and broken. GTK has native macOS support.