J Eti J Eti - 3 months ago 15
C Question

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

I want to see some specifications for some functions and structs. In the tutorial I'm going through, it says

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

I remember that I once (by accident) opened the file for math.h but I've looked for them with the

find -name
find -wholename

commands in Bash and haven't been able to, and internet hasn't solved my problems.


If you are using a system where gcc is installed, you can get the included C preprocessor to print out the complete paths of all include files. Of course, that assumes that you have the header file installed in such a way that gcc can find it.

For a simple system header file, you can extract the path easily:

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

Note 1: The |&head -n1 causes only the first line of the output -- which is directed to stderr -- to be printed. The full output contains the entire include tree, with dots indicating include depth.

Note 2: The above assumes that you are using C. If the header is C++, you need to tell cpp, using the -x flag:

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

For a library whose header files require an -I option, you could try using pkg-config to find the correct compiler option (assuming you know the correct package name):

$ 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

To find the package name on my system, I queried pkg-config itself, and then guessed which one was desired:

$ 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)