Ubuntu24.04编译GTK4应用遇virtual:world错误,求解决方案
问题:Ubuntu 24.04编译GTK4 Hello World程序失败
我正在学习GTK,尝试在Ubuntu 24.04系统上编译其hello world示例程序,但执行编译命令时出现以下错误:
> gcc $(pkg-config --cflags gtk4) -o hello-world-gtk hello-world-gtk.c $(pkg-config --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 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.
我以为已经安装了GTK相关包,因为执行sudo apt install libgtk-4-1时,系统提示该包已是最新版本:
> sudo apt install libgtk-4-1 Reading package lists... Done Building dependency tree... Done Reading state information... Done libgtk-4-1 is already the newest version (4.14.2+ds-1ubuntu1). libgtk-4-1 set to manually installed. The following packages were automatically installed and are no longer required: linux-headers-6.8.0-50 linux-headers-6.8.0-50-generic linux-image-6.8.0-50-generic linux-modules-6.8.0-50-generic linux-modules-extra-6.8.0-50-generic linux-tools-6.8.0-50 linux-tools-6.8.0-50-generic python3-netifaces Use 'sudo apt autoremove' to remove them. 0 upgraded, 0 newly installed, 0 to remove and 127 not upgraded.
问题原因与解决办法
问题原因
你安装的libgtk-4-1是GTK4的运行时库,仅用于运行已经编译好的GTK4程序,不包含编译程序所需的头文件(比如gtk/gtk.h)和pkg-config配置文件(gtk4.pc),这就是编译时找不到依赖的核心原因。
解决步骤
- 安装GTK4的开发包,执行以下命令:
sudo apt install libgtk-4-dev
- 安装完成后,重新执行原来的编译命令即可正常编译:
gcc $(pkg-config --cflags gtk4) -o hello-world-gtk hello-world-gtk.c $(pkg-config --libs gtk4)
libgtk-4-dev包包含了编译GTK4程序必需的所有开发文件,安装后pkg-config就能找到gtk4.pc配置,编译器也能定位到gtk/gtk.h头文件,编译错误即可解决。
内容的提问来源于stack exchange,提问作者Cinverse
相关产品推荐
相关产品推荐

