Home » C++ » What are the GCC default include directories?

What are the GCC default include directories?

Posted by: admin November 29, 2017 Leave a comment

Questions:

When i compile a very simple source file with gcc I don’t have to specify the path to standard include files such as stdio or stdlib.

How GCC knows how to find these files?

Does it have the /usr/include path hardwired inside, or it will get the paths from other OS components?

Answers:

In order to figure out the default paths used by gcc/g++ as well as their priorities you examine the output of the following commands:

  1. For C:

    gcc -xc -E -v -
    
  2. For C++:

    gcc -xc++ -E -v -
    

The credit goes to Qt Creator team.

Questions:
Answers:

There is a command with a shorter output, which allows to automatically cut the include pathes from lines, starting with a single space:

$ echo | gcc -Wp,-v -x c++ - -fsyntax-only
ignoring nonexistent directory "/usr/lib/gcc/x86_64-redhat-linux/4.8.2/include-fixed"
ignoring nonexistent directory "/usr/lib/gcc/x86_64-redhat-linux/4.8.2/../../../../x86_64-redhat-linux/include"
#include "..." search starts here:
#include <...> search starts here:
 /usr/lib/gcc/x86_64-redhat-linux/4.8.2/../../../../include/c++/4.8.2
 /usr/lib/gcc/x86_64-redhat-linux/4.8.2/../../../../include/c++/4.8.2/x86_64-redhat-linux
 /usr/lib/gcc/x86_64-redhat-linux/4.8.2/../../../../include/c++/4.8.2/backward
 /usr/lib/gcc/x86_64-redhat-linux/4.8.2/include
 /usr/local/include
 /usr/include
End of search list.

The credit goes to the libc++ front-page.

Questions:
Answers:

Here is an article describing the gcc search paths: http://www.network-theory.co.uk/docs/gccintro/gccintro_21.html

In addition, you can specify extra search paths with the -I switch. You can do this from the command line or in your makefile.