在 pkg-config 搜索路径中找不到包 gtk4

Package gtk4 was not found in the pkg-config search path

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

pacman -S mingw-w64-x86_64-gtk3

(以上根据 Setting up GTK for Windows步骤 2)和

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

但是当我尝试用

编译hello world
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

the above as per Step 2 of Setting up GTK for Windows

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

实际上我不得不设置 following package

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. 

查看 this issue 我尝试将 C:\msys64\mingw64\bin 添加到我的系统 PATH:它没有显示 运行time 错误,但 exe 确实无法运行无论如何(应用程序没有按预期显示)。

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

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

和上面的 编译并且 运行s 很好。

解决运行时间问题

否则,可以选择 build GTK4 on Windows from source 不使用 MSYS2

这张纸条很重要

It works out of the box on my VirtualBox machine, however on my physical PC with an intel GPU, I get a crash at startup:

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

This can be worked around by using cairo rendering:

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
  • PATH系统环境变量的开头添加C:\msys64\mingw64\bin;
  • 添加一个新的 GSK_RENDERER 系统环境变量,值为 cairo
  • 使用 MSYS2 MinGW 64 位 shell 编译(作为 GTK4)和 运行 exe