Where does gcc look for C and C++ header files?

前端 未结 9 1706
悲&欢浪女
悲&欢浪女 2020-11-28 17:26

On a Unix system, where does gcc look for header files?

I spent a little time this morning looking for some system header files, so I thought this would be good info

相关标签:
9条回答
  • 2020-11-28 17:54

    The CPP Section of the GCC Manual indicates that header files may be located in the following directories:

    GCC looks in several different places for headers. 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
    

    For C++ programs, it will also look in /usr/include/g++-v3, first.

    0 讨论(0)
  • 2020-11-28 17:55

    To get GCC to print out the complete set of directories where it will look for system headers, invoke it like this:

    $ LC_ALL=C gcc -v -E -xc - < /dev/null 2>&1 | 
      LC_ALL=C sed -ne '/starts here/,/End of/p'
    

    which will produce output of the form

    #include "..." search starts here:
    #include <...> search starts here:
     /usr/lib/gcc/x86_64-linux-gnu/5/include
     /usr/local/include
     /usr/lib/gcc/x86_64-linux-gnu/5/include-fixed
     /usr/include/x86_64-linux-gnu
     /usr/include
    End of search list.
    

    If you have -I-family options on the command line they will affect what is printed out.

    (The sed command is to get rid of all the other junk this invocation prints, and the LC_ALL=C is to ensure that the sed command works -- the "starts here" and "End of search list" phrases are translated IIRC.)

    0 讨论(0)
  • 2020-11-28 17:56

    In addition, gcc will look in the directories specified after the -I option.


    0 讨论(0)
  • 2020-11-28 17:58

    You can create a file that attempts to include a bogus system header. If you run gcc in verbose mode on such a source, it will list all the system include locations as it looks for the bogus header.

    $ echo "#include <bogus.h>" > t.c; gcc -v t.c; rm t.c
    
    [..]
    
    #include "..." search starts here:
    #include <...> search starts here:
     /usr/local/include
     /usr/lib/gcc/i686-apple-darwin9/4.0.1/include
     /usr/include
     /System/Library/Frameworks (framework directory)
     /Library/Frameworks (framework directory)
    End of search list.
    
    [..]
    
    t.c:1:32: error: bogus.h: No such file or directory
    
    0 讨论(0)
  • 2020-11-28 18:01

    The set of paths where the compiler looks for the header files can be checked by the command:-

    cpp -v

    If you declare #include "" , the compiler first searches in current directory of source file and if not found, continues to search in the above retrieved directories.

    If you declare #include <> , the compiler searches directly in those directories obtained from the above command.

    Source:- http://commandlinefanatic.com/cgi-bin/showarticle.cgi?article=art026

    0 讨论(0)
  • 2020-11-28 18:03
    g++ -print-search-dirs
    gcc -print-search-dirs
    
    0 讨论(0)
提交回复
热议问题