首页 > 解决方案 > 在 pkg-config 搜索路径中找不到包 gtk4

问题描述

我已经在 Windows 7 上安装了 MSYS2 并且我已经成功执行

pacman -S mingw-w64-x86_64-gtk3

(以上按照为 Windows 设置 GTK 的2 步)和

pacman -S mingw-w64-x86_64-toolchain base-devel

但是当我尝试

gcc -o hello-world-gtk hello-world-gtk.c `pkg-config --cflags --libs gtk4`

我得到以下

Package gtk4 was not found in the pkg-config search path.
Perhaps you should add the directory containing `gtk4.pc'
to the PKG_CONFIG_PATH environment variable
Package 'gtk4', required by 'virtual:world', not found
bash: gcc: command not found

如果我尝试从 MinGW 64 位 shell 输出是

MyHome@MyHome-PC MINGW64 ~
$ gcc -o hello-world-gtk hello-world-gtk.c `pkg-config --cflags --libs gtk4`
Package gtk4 was not found in the pkg-config search path.
Perhaps you should add the directory containing `gtk4.pc'
to the PKG_CONFIG_PATH environment variable
Package 'gtk4', required by 'virtual:world', not found
hello-world-gtk.c:1:10: fatal error: gtk/gtk.h: No such file or directory
    1 | #include <gtk/gtk.h>
      |          ^~~~~~~~~~~
compilation terminated.

我也检查过

$ echo $PKG_CONFIG_PATH
/mingw64/lib/pkgconfig:/mingw64/share/pkgconfig

标签: windowsgccgtkmsys2

解决方案


以上按照为 Windows 设置 GTK 的第 2 步

他们的网站上似乎有一些过时的说明

我实际上不得不设置以下包

pacman -S mingw-w64-x86_64-gtk4

几个可能的运行时问题

然后我可以编译这个例子,但它运行不正常。我仍然收到运行时错误:

---------------------------
hello-world-gtk.exe - Errore di sistema
---------------------------
Impossibile avviare il programma perché libgio-2.0-0.dll non è presente nel computer. Per risolvere il problema, provare a reinstallare il programma. 

查看这个问题,我尝试将其添加C:\msys64\mingw64\bin到我的 sysPATH中:它没有显示运行时错误,但 exe 确实无法正常工作(应用程序未按预期显示)。

AFAICS gtk4-demo-application本身在我的 Windows 上运行不正常,所以最后我不得不恢复到 GTK3,我编译了GTK3 入门的示例

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

以上编译并运行良好。

运行时问题的解决方案

否则,可以选择在没有 MSYS2的情况下从源代码在 Windows 上构建 GTK4

这条注释非常重要

它在我的 VirtualBox 机器上开箱即用,但是在我的带有英特尔 GPU 的物理 PC 上,我在启动时遇到了崩溃:

Unhandled exception at 0x5211345E (ig4icd32.dll) in gtk4-demo.exe: 0xC0000005: Access violation reading location 0x00000050

这可以通过使用 cairo 渲染来解决:

C:\src\gtk>set GSK_RENDERER=cairo
C:\src\gtk>C:\gnome\bin\gtk4-demo.exe

结论:MSYS2 上 GTK4 的程序

总之set GSK_RENDERER=cairo,同样的修复也可以运行在 MSYS2 下编译的可执行文件,因此最终解决方案包括

  • 安装pacman -S mingw-w64-x86_64-gtk4
  • 在系统环境变量C:\msys64\mingw64\bin;的开头添加PATH
  • 添加GSK_RENDERER具有值的新系统环境变量cairo
  • 使用 MSYS2 MinGW 64 位 shell 编译(作为 GTK4)并运行 exe

推荐阅读