Programming

gcc는 C 및 C ++ 헤더 파일을 어디에서 찾습니까?

procodes 2020. 5. 26. 21:41
반응형

gcc는 C 및 C ++ 헤더 파일을 어디에서 찾습니까?


유닉스 시스템에서 gcc는 헤더 파일을 어디에서 찾습니까?

오늘 아침에 약간의 시스템 헤더 파일을 찾기 위해 시간을 보냈으므로 여기에 유용한 정보라고 생각했습니다.


`gcc -print-prog-name=cc1plus` -v

이 명령은 gcc에게 사용중인 C ++ 전처리기를 요청한 다음 포함 할 전처리기를 묻습니다.

특정 설정에 대한 신뢰할 수있는 답변을 얻을 수 있습니다.

마찬가지로 C 전 처리기의 경우 :

`gcc -print-prog-name=cpp` -v

또한 gcc는 -I옵션 뒤에 지정된 디렉토리를 찾습니다 .



가짜 시스템 헤더를 포함시키려는 파일을 작성할 수 있습니다. 이러한 소스에서 상세 모드로 gcc를 실행하면 가짜 헤더를 찾는 모든 시스템 포함 위치가 나열됩니다.

$ echo "#include <bogus.h> int main(){}" > 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

GCC 매뉴얼CPP 섹션헤더 파일이 다음 디렉토리에있을 수 있음을 나타냅니다.

GCC는 여러 곳에서 헤더를 찾습니다. 일반적인 유닉스 시스템에서 달리 지시하지 않으면 #include에서 요청한 헤더를 찾습니다.

 /usr/local/include
 libdir/gcc/target/version/include
 /usr/target/include
 /usr/include

C ++ 프로그램의 경우 먼저 / usr / include / g ++-v3을 살펴 봅니다.


GCC가 시스템 헤더를 찾을 디렉토리의 전체 세트를 인쇄하게하려면 다음과 같이 호출하십시오.

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

양식의 출력을 생성합니다

#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.

-I명령 행에 -family 옵션 이 있으면 출력되는 내용에 영향을줍니다.

(이 sed명령은 이 호출이 인쇄 하는 다른 모든 정크를 제거 LC_ALL=C하고 sed명령이 작동 하는지 확인하는 것입니다 . "여기에서 시작"및 "검색 목록 끝"문구 IIRC 번역됩니다.


g++ -print-search-dirs
gcc -print-search-dirs

컴파일러가 헤더 파일을 찾는 경로 세트는 다음 명령으로 확인할 수 있습니다.

cpp -v

#include "" 을 선언 하면 컴파일러는 먼저 소스 파일의 현재 디렉토리를 검색하고 찾지 못한 경우 위의 검색된 디렉토리에서 계속 검색합니다.

#include <> 를 선언 하면 컴파일러는 위 명령에서 얻은 디렉토리에서 직접 검색합니다.

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


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

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.

참고URL : https://stackoverflow.com/questions/344317/where-does-gcc-look-for-c-and-c-header-files

반응형