1

Is there a definitive way to see where a given #include <example.h> resolves to? I have a #include <linux/unistd.h> in my code but I don't know which unistd.h is being used.

3
  • 1
    Does it matter which unistd? That file tends to be fairly standard: pubs.opengroup.org/onlinepubs/009695399/basedefs/unistd.h.html Commented Feb 13, 2011 at 5:05
  • So that's what the std stands for :P Commented Feb 13, 2011 at 5:11
  • Including <linux/unistd.h> instead of simply <unistd.h> is most probably a bug. Commented Feb 13, 2011 at 5:33

2 Answers 2

1

If you use the -E command line option to get the preprocessor output, it will tell you the full path to every header file included, including those included by other headers. For example:

$ cat test.c #include <unistd.h> $ gcc -E test.c # 1 "test.c" # 1 "<built-in>" # 1 "<command-line>" # 1 "test.c" # 1 "/usr/include/unistd.h" 1 3 4 # 71 "/usr/include/unistd.h" 3 4 # 1 "/usr/include/_types.h" 1 3 4 # 27 "/usr/include/_types.h" 3 4 # 1 "/usr/include/sys/_types.h" 1 3 4 # 32 "/usr/include/sys/_types.h" 3 4 # 1 "/usr/include/sys/cdefs.h" 1 3 4 # 33 "/usr/include/sys/_types.h" 2 3 4 # 1 "/usr/include/machine/_types.h" 1 3 4 # 34 "/usr/include/machine/_types.h" 3 4 # 1 "/usr/include/i386/_types.h" 1 3 4 # 37 "/usr/include/i386/_types.h" 3 4 typedef signed char __int8_t; (lots more output) 

So in this case, the header that's being used is /usr/include/unistd.h.

Sign up to request clarification or add additional context in comments.

Comments

0

From the GCC documentation,

On a normal Unix system, if you do not instruct it otherwise, it will look for headers requested with #include in:

 /usr/local/include libdir/gcc/target/version/include /usr/target/include /usr/include 

In the above, target is the canonical name of the system GCC was configured to compile code for; often but not always the same as the canonical name of the system it runs on. version is the version of GCC in use.

You can add to this list with the -Idir command line option. All the directories named by -I are searched, in left-to-right order, before the default directories. The only exception is when dir is already searched by default. In this case, the option is ignored and the search order for system directories remains unchanged.

So unless you're adding the -I (capital eye, not ell) switch, the version included is the version found in the first of those directories that holds it.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.