如何查看 C 头文件的源代码?

How do I see the source code of the C header files?

我想查看一些函数和结构的规范。在我正在学习的教程中,它说

see the gdk/gdkkeysyms.h header file for a complete list of GDK key codes.

我记得我曾经(不小心)打开了 math.h 的文件,但我用

find -name 
find -wholename

Bash 中的命令但无法执行,互联网也没有解决我的问题。

您可以简单地使用命令:

find /usr/include -name gdkkeysyms.h

在你的 bash 中。 Unix 系统资源目录下的 include 文件夹(usr/usr/localopt 取决于它的安装方式)目录包含您可能需要的所有目录。每当您想查看源代码时,请尝试使用此语法:

find /usr/include -name header_name.h

参考资料

How to view source code of header file in C++?

注意(对于 C++):

C++ headers would be in /usr/include/c++

如果您使用的系统安装了gcc,您可以获得包含的C预处理器来打印出所有包含文件的完整路径。当然,前提是您以 gcc 可以找到的方式安装了头文件。

对于一个简单的系统头文件,你可以很容易地提取路径:

$ cpp -H -o /dev/null - <<<'#include <sys/time.h>' |&head -n1
. /usr/include/x86_64-linux-gnu/sys/time.h

注释 1|&head -n1 仅导致输出的第一行——定向到 stderr——被打印。完整输出包含整个包含树,点表示包含深度。

注2:上面假设你使用的是C,如果header是C++,你需要告诉cpp,使用-x标志:

$ cpp -H -o /dev/null -x c++ - <<<'#include <vector>' |&head -n1
. /usr/include/c++/5/vector

对于头文件需要 -I 选项的库,您可以尝试使用 pkg-config 找到正确的编译器选项(假设您知道正确的包名称):

$ cpp -H -o /dev/null $(pkg-config --cflags gdk-2.0) - <<<'#include <gdk/gdkkeysyms.h>'|&head -n1
. /usr/include/gtk-2.0/gdk/gdkkeysyms.h

为了在我的系统上查找软件包名称,我查询了 pkg-config 本身,然后猜测需要哪个:

$ pkg-config --list-all | grep gdk
gdk-pixbuf-2.0                 GdkPixbuf - Image loading and scaling
gdk-2.0                        GDK - GTK+ Drawing Kit (x11 target)
gdk-pixbuf-xlib-2.0            GdkPixbuf Xlib - GdkPixbuf rendering for Xlib
gdk-x11-2.0                    GDK - GTK+ Drawing Kit (x11 target)