Search code examples
c++clinuxgccinclude-path

What are the GCC default include directories?


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 does GCC know how to find these files?

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


Solution

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

    1. For C:
    echo | gcc -xc -E -v -
    
    1. For C++:
    echo | gcc -xc++ -E -v -
    

    The credit goes to Qt Creator team.

    Here's a breakdown of the flags:

    • -x selects the language, C or C++ respectively

    • -E makes gcc to run the preprocessor only, so no compilation takes place

    • -v prints all the commands run, which is the key to dumping the standard paths

    • - is the "input file" to preprocess, as a convention - stands for stdin (or stdout, depending on the context);

      echo | feeds an empty string to gcc so effectively we preprocess an empty file generated on the fly

    Here's a nice explaining it in more detail: https://explainshell.com/explain?cmd=echo+%7C+gcc+-xc+-E+-v+-