尝试将 GTK4 与 Rust 一起使用时,编译并运行使用 Rust 和 GTK 4 进行 GUI 开发的 Hello World 示例失败,并出现以下错误:
$ 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 已按照 MacOS
说明使用
brew
安装,并按照 项目设置 配置了 rust 项目。
这是 Cargo.toml
中生成的 GTK 依赖项:
gtk = { version = "0.9.2", package = "gtk4", features = ["v4_16"] }
观察结果pango
等。
/opt/homebrew/lib/pkgconfig
包含
gobject-2.0.pc
包文件。
gobject-2.0.pc
包文件包含以下内容:
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的源文件是否存在于包文件中指示的头文件和lib目录中,但这些都是以
gobject
可以在前缀目录中找到:
$ 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
对于如何解决这个问题有什么建议吗?
运行需要设置PKG_CONFIG_PATH。如果不存在,还有许多其他依赖项也会失败,例如 pango 等。在我看来,这就是问题所在,您的冲泡设置不正确。这不是应该发生的事情,需要指定 pkg 配置路径。
运行以下命令:
brew --prefix pkg-config
如果它返回的内容与您提供的路径不同,例如/opt/homebrew/share/pkgconfig
,请按如下方式添加,否则您可以仅添加您使用的路径:
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
您可能需要重新打开终端,您可以使用进行验证
echo $PKG_CONFIG_PATH
然后再试一次。