c/c++ header 파일 중복 include 에러(error) 방지 - #ifndef 사용


c/c++ header 파일 중복 include 에러(error) 방지 - #ifndef 사용

1. test.h int num = 0; 2. calc.h #include "test.h" ... (생략) 3. calc.c #include test.h #include calc.h ...(생략) -------------------- 위와 같은 형태로 되어있을 경우 int num = 0; 이 두번 선언이 되기 때문에 재정의 에러가 발생한다. 이것을 방지 하고자 할 경우에는 1. test.h #ifndef __test_h__ #define __test_h__ int num = 0; #endif 2. calc.h #ifndef __calc_h__ #define __calc_h__ #include "test.h" ...(생략) #endif 와 같이 header 파일을 수정한다. 1번 기준으로 설명하면, #ifndef는 컴파일 단계에서 __test_h__ 가 선언되어있는지 확인한다. 처음에는 선언되어있지 않기 때문에 "true"로 판단하여 진행하고 바로 다음줄에서 #define __test...


#IT·컴퓨터

원문링크 : c/c++ header 파일 중복 include 에러(error) 방지 - #ifndef 사용