在Arch Linux上编译GTK4程序时GCC无法找到gtk/gtk.h的问题求助
在Arch Linux上编译GTK4程序时GCC无法找到gtk/gtk.h的问题求助
我正在尝试运行GTK官方文档里的"Hello World"示例,按照指导执行了编译命令,结果遇到了GCC找不到头文件的错误,具体情况如下:
我执行的编译命令是:
gcc $(pkg-config --cflags gtk4) -o hello-world-gtk hello-world-gtk.c $(pkg-config --libs gtk4)
但却得到了下面的编译错误:
hello-world-gtk.c:1:10: fatal error: gtk/gtk.h: No such file or directory 1 | #include <gtk/gtk.h> | ^~~~~~~~~~~ compilation terminated.
我已经安装了GTK4,而且/usr/include/gtk-4.0/gtk目录确实存在,gtk.h文件也在里面。明明用了pkg-config来传递编译参数,GCC却还是找不到这个头文件,这让我很困惑。
额外信息
- 系统环境:Arch Linux,内核版本
6.11.9-arch1-1 - 已安装的GTK版本:
gtk4(通过pacman -Q gtk4确认) - pkg-config测试正常:执行
pkg-config --cflags gtk4能输出类似这样的包含路径参数:
执行-I/usr/include/gtk-4.0pkg-config --libs gtk4也能输出正确的链接器参数。
我已经尝试的解决步骤
- 确认
/usr/include/gtk-4.0/gtk/gtk.h文件确实存在且可访问 - 通过
pacman重新安装了gtk4和pkg-config - 验证了
pkg-config能输出正确的编译和链接参数
根据建议,我又补充了以下排查信息:
- 确认gtk.h存在且可访问:
执行文件权限检查后得到输出:.rw-r--r-- 11k root 1 Nov 20:21 /usr/include/gtk-4.0/gtk/gtk.h - 展开后的完整编译命令:
把pkg-config的参数展开后,完整的编译命令是:gcc -I/usr/include/gtk-4.0 -I/usr/include/pango-1.0 -I/usr/include/harfbuzz -I/usr/include/gdk-pixbuf-2.0 -I/usr/include/cairo -I/usr/include/glib-2.0 -I/usr/lib/glib-2.0/include -I/usr/include/freetype2 -I/usr/include/graphene-1.0 -I/usr/lib/graphene-1.0/include -mfpmath=sse -msse -msse2 -I/usr/include/libmount -I/usr/include/blkid -I/usr/include/fribidi -I/usr/include/sysprof-6 -pthread -I/usr/include/libpng16 -I/usr/include/pixman-1 -o hello-world-gtk hello-world-gtk.c -lgtk-4 -lharfbuzz -lpangocairo-1.0 -lpango-1.0 -lgdk_pixbuf-2.0 -lcairo-gobject -lcairo -lvulkan -lgraphene-1.0 -lgio-2.0 -lglib-2.0 -lgobject-2.0 - 直接添加-I参数的测试结果:
尝试手动给GCC加上-I/usr/include/gtk-4.0参数编译时,又出现了新的错误:fatal error: glib.h: No such file or directory - pkg-config的完整cflags输出:
执行pkg-config --cflags gtk4确实包含了glib的头文件路径:-I/usr/include/glib-2.0 -I/usr/lib/glib-2.0/include - 环境变量检查:
PKG_CONFIG_PATH环境变量为空,不会干扰pkg-config的正常工作:echo $PKG_CONFIG_PATH (no output) - GCC版本信息:
执行gcc -v输出如下:Using built-in specs. COLLECT_GCC=gcc COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-pc-linux-gnu/14.2.1/lto-wrapper Target: x86_64-pc-linux-gnu Configured with: /build/gcc/src/gcc/configure --enable-languages=ada,c,c++,d,fortran,go,lto,m2,objc,obj-c++,rust --enable-bootstrap --prefix=/usr --libdir=/usr/lib --libexecdir=/usr/lib --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=https://gitlab.archlinux.org/archlinux/packaging/packages/gcc/-/issues --with-build-config=bootstrap-lto --with-linker-hash-style=gnu --with-system-zlib --enable-__cxa_atexit --enable-cet=auto --enable-checking=release --enable-clocale=gnu --enable-default-pie --enable-default-ssp --enable-gnu-indirect-function --enable-gnu-unique-object --enable-libstdcxx-backtrace --enable-link-serialization=1 --enable-linker-build-id --enable-lto --enable-multilib --enable-plugin --enable-shared --enable-threads=posix --disable-libssp --disable-libstdcxx-pch --disable-werror Thread model: posix Supported LTO compression algorithms: zlib zstd gcc version 14.2.1 20240910 (GCC) - GCC的包信息:
执行pacman -Qi gcc输出如下:Name : gcc Version : 14.2.1+r134+gab884fffe3fc-1 Description : The GNU Compiler Collection - C and C++ frontends Architecture : x86_64 URL : https://gcc.gnu.org Licenses : GPL-3.0-with-GCC-exception GFDL-1.3-or-later Groups : None Provides : gcc-multilib Depends On : gcc-libs=14.2.1+r134+gab884fffe3fc-1 binutils>=2.28 libmpc zstd libisl.so=23-64 Optional Deps : lib32-gcc-libs: for generating code for 32-bit ABI Required By : base-devel clang dmd rust vala Optional For : dmd node-gyp xorg-xrdb Conflicts With : None Replaces : gcc-multilib Installed Size : 202,47 MiB Packager : Frederik Schwan <freswa@archlinux.org> Build Date : 2024-09-10T15:07:33 CEST Install Date : 2024-11-09T22:09:28 CET Install Reason : Installed as a dependency for another package Install Script : No Validated By : Signature
请问我到底忽略了什么?该怎么解决这个编译错误呢?另外clangd也遇到了同样的问题,因为找不到头文件而输出大量错误。
备注:内容来源于stack exchange,提问作者domi




