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

前端 未结 9 1707
悲&欢浪女
悲&欢浪女 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 18:04

    One could view the (additional) include path for a C program from bash by checking out the following:

    echo $C_INCLUDE_PATH
    

    If this is empty, it could be modified to add default include locations, by:

    export C_INCLUDE_PATH=$C_INCLUDE_PATH:/usr/include
    
    0 讨论(0)
  • 2020-11-28 18:09
    `gcc -print-prog-name=cc1plus` -v
    

    This command asks gcc which C++ preprocessor it is using, and then asks that preprocessor where it looks for includes.

    You will get a reliable answer for your specific setup.

    Likewise, for the C preprocessor:

    `gcc -print-prog-name=cpp` -v
    
    0 讨论(0)
  • 2020-11-28 18:10

    These are the directories that gcc looks in by default for the specified header files ( given that the header files are included in chevrons <>); 1. /usr/local/include/ --used for 3rd party header files. 2. /usr/include/ -- used for system header files.

    If in case you decide to put your custom header file in a place other than the above mentioned directories, you can include them as follows: 1. using quotes ("./custom_header_files/foo.h") with files path, instead of chevrons in the include statement. 2. using the -I switch when compiling the code. gcc -I /home/user/custom_headers/ -c foo.c -p foo.o Basically the -I switch tells the compiler to first look in the directory specified with the -I switch ( before it checks the standard directories).When using the -I switch the header files may be included using chevrons.

    0 讨论(0)
提交回复
热议问题