Reputation: 1508
When trying to use GTK4 with Rust compiling and running the Hello World example of the GUI development with Rust and GTK 4 fails with the following error:
$ PKG_CONFIG_PATH="/opt/homebrew/lib/pkgconfig" cargo run
...
> PKG_CONFIG_PATH=/opt/homebrew/lib/pkgconfig PKG_CONFIG_ALLOW_SYSTEM_CFLAGS=1 pkg-config --libs --cflags gobject-2.0 gobject-2.0 >= 2.62
The system library `gobject-2.0` required by crate `glib-sys` was not found.
The file `gobject-2.0.pc` needs to be installed and the PKG_CONFIG_PATH environment variable must contain its parent directory.
PKG_CONFIG_PATH contains the following:
- /opt/homebrew/lib/pkgconfig
HINT: you may need to install a package such as gobject-2.0, gobject-2.0-dev or gobject-2.0-devel.
GTK has been installed using brew
as per the MacOS instructions and the rust project configured as per the Project Setup.
This is the resulting GTK dependency in Cargo.toml
:
gtk = { version = "0.9.2", package = "gtk4", features = ["v4_16"] }
pango
etc./opt/homebrew/lib/pkgconfig
contains the gobject-2.0.pc
package file.gobject-2.0.pc
package file contains the following:prefix=/opt/homebrew/Cellar/glib/2.82.1
includedir=${prefix}/include
libdir=${prefix}/lib
Name: GObject
Description: GLib Type, Object, Parameter and Signal Library
Version: 2.82.1
Requires: glib-2.0
Requires.private: libffi >= 3.0.0
Libs: -L${libdir} -lgobject-2.0
Libs.private: -lintl
Cflags: -I${includedir}
gobject-2.0
lib are present or not in the header and lib directories indicated in the package file, but these are all files starting with gobject
that can be found in the prefix dir:$ find /opt/homebrew/Cellar/glib/2.82.1 -name 'gobject*'
/opt/homebrew/Cellar/glib/2.82.1/bin/gobject-query
/opt/homebrew/Cellar/glib/2.82.1/include/glib-2.0/gobject
/opt/homebrew/Cellar/glib/2.82.1/include/glib-2.0/gobject/gobjectnotifyqueue.c
/opt/homebrew/Cellar/glib/2.82.1/include/glib-2.0/gobject/gobject-visibility.h
/opt/homebrew/Cellar/glib/2.82.1/include/glib-2.0/gobject/gobject-autocleanups.h
/opt/homebrew/Cellar/glib/2.82.1/include/glib-2.0/gobject/gobject.h
/opt/homebrew/Cellar/glib/2.82.1/lib/pkgconfig/gobject-2.0.pc
/opt/homebrew/Cellar/glib/2.82.1/share/glib-2.0/gdb/gobject_gdb.py
Any suggestions on how to resolve the issue?
Upvotes: 1
Views: 605
Reputation: 1508
Running the command echoed in the error output gave additional details on missing dependencies:
PKG_CONFIG_PATH=/opt/homebrew/lib/pkgconfig PKG_CONFIG_ALLOW_SYSTEM_CFLAGS=1 pkg-config --libs --cflags gobject-2.0 gobject-2.0 >= 2.62
Adding the missing dependency through brew
and running the command again repeateadly, revealed all missing dependencies, step-by-step.
brew install gtk4 libffi zlib expat proto
It would appear that the libffi
, zlib
, and expat
dependencies are MacOS libs and are not added to the pkg-config
path in the shared homebrew configuration directory and thus must be added explicitly to the PKG_CONFIG_PATH
, in my case the following paths work:
PKG_CONFIG_PATH="/opt/homebrew/lib/pkgconfig"
PKG_CONFIG_PATH="/opt/homebrew/opt/pkg-config:$PKG_CONFIG_PATH"
PKG_CONFIG_PATH="/opt/homebrew/opt/libffi/lib/pkgconfig:$PKG_CONFIG_PATH"
PKG_CONFIG_PATH="/opt/homebrew/opt/zlib/lib/pkgconfig:$PKG_CONFIG_PATH"
PKG_CONFIG_PATH="/opt/homebrew/opt/expat/lib/pkgconfig:$PKG_CONFIG_PATH"
PKG_CONFIG_PATH="/opt/homebrew/share/pkgconfig:$PKG_CONFIG_PATH"
export PKG_CONFIG_PATH
I have added this to my .zshrc
configuration. Note that the last path added is a generic path required by proto
which was found by locating the xproto.pc
reported as missing by cargo
.
I am not clear why pkg-config
seems to include both the gtk4
and Mac OS X system libraries I had to install manually, for others but not on my system.
Thanks to @avifen and @jmb for helping me resolve the issue!
Upvotes: 1
Reputation: 1017
Running requires setting the PKG_CONFIG_PATH. If not present, there is a host of other dependencies that fail as well, pango etc.
That's seem to me to be the problem, something with your brew settings is not right. It's not something that suppose to happen, needing to specify the pkg config path.
Run bellow commands:
brew --prefix pkg-config
In case it returns something different than the path you provided, for example /opt/homebrew/share/pkgconfig
add it as bellow, otherwise you can add just the path you use:
export PKG_CONFIG_PATH="/opt/homebrew/lib/pkgconfig:/opt/homebrew/share/pkgconfig:$PKG_CONFIG_PATH"
export PATH="/opt/homebrew/bin:$PATH"
source ~/.zshrc # or ~/.bashrc depending on your shell
You may need to reopen your terminal, and you can validate with
echo $PKG_CONFIG_PATH
Then try again.
Upvotes: 0