1

I want to begin my question with a disclaimer that I am beginner with c++.

My question is how does the preprocessor know which directories to look for when looking for header file.

I know that it searches in some specific predefined locations and whatever we pass as -I in the g++ compilation step. However what confuses me is some standard headers are picked up even if they are not in those locations.

I referred to the question at Finding out what the GCC include path is and I followed the steps there.

$ /usr/include % echo | cpp -Wp,-v 
ignoring nonexistent directory "/usr/local/include/x86_64-linux-gnu"
ignoring nonexistent directory "/usr/lib/gcc/x86_64-linux-gnu/4.9/../../../../x86_64-linux-gnu/include"
#include "..." search starts here:
#include <...> search starts here:
 /usr/lib/gcc/x86_64-linux-gnu/4.9/include
 /usr/local/include
 /usr/lib/gcc/x86_64-linux-gnu/4.9/include-fixed
 /usr/include/x86_64-linux-gnu
 /usr/include
End of search list.
# 1 "<stdin>"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "/usr/include/stdc-predef.h" 1 3 4
# 1 "<command-line>" 2
# 1 "<stdin>"

But I tried to locate the iostream and I see

$ locate iostream
/usr/include/c++/4.8/iostream

it is located at /usr/include/c++/4.8/iostream , but the search path ends at /usr/include . If it were any other header file, I would have to include it as #include <c++/4.8/iostream>, but in all the programs I use #include <iostream> .

PS: The reason I am asking this question is because I accidentally deleted /usr/include/c++/4.9 directory, it used to work before deleting. But it is not picking up usr/include/c++/4.8/cstddef . I want to understand the procedure for picking up correct include paths of c++ preprocessor.

Thanks

Community
  • 1
  • 1
Anoop
  • 5,540
  • 7
  • 35
  • 52
  • I don't remember where I read it, so this might need to be taken with a grain of salt: Standard include files don't even need to be files in disk. How the compiler treats those is totally up to whoever made the compiler. The point is, for example, if you `#include `, you'll have access to everything that's supposed to bring in with it. – Steve Jul 31 '15 at 19:53

1 Answers1

2

They're compiled in. Make sure you specify the correct language.

$ echo | cpp -Wp,-v -x c++
ignoring nonexistent directory "/usr/lib/gcc/x86_64-redhat-linux/4.9.2/include-fixed"
ignoring nonexistent directory "/usr/lib/gcc/x86_64-redhat-linux/4.9.2/../../../../x86_64-redhat-linux/include"
#include "..." search starts here:
#include <...> search starts here:
 /usr/lib/gcc/x86_64-redhat-linux/4.9.2/../../../../include/c++/4.9.2
 /usr/lib/gcc/x86_64-redhat-linux/4.9.2/../../../../include/c++/4.9.2/x86_64-redhat-linux
 /usr/lib/gcc/x86_64-redhat-linux/4.9.2/../../../../include/c++/4.9.2/backward
 /usr/lib/gcc/x86_64-redhat-linux/4.9.2/include
 /usr/local/include
 /usr/include
End of search list.
# 1 "<stdin>"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "/usr/include/stdc-predef.h" 1 3 4
# 1 "<command-line>" 2
# 1 "<stdin>"
Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358