showTitle("https://tistory1.daumcdn.net/tistory/520789/skin/images/", "viewTitle.swf", 600, 30, "#pragma once (why it is always attached when we making new class file in visual studio?", "/entry/pragma-once-why-it-is-always-attached-when-we-making-new-class-file-in-visual-studio", "left", "#000000");
2010. 3. 28. 06:41
Programming/C++
#pragma once
Specifies that the file will be included (opened) only once by the compiler in a build. This can reduce build times as the compiler will not open and read the file after the first #include of the module.
For example,
// header.h
#pragma once
C의 헤더 파일 최상단에서 자주 볼 수 있는 이 코드는 컴파일러에게 이 헤더 파일이 한번만 빌드되도록 알려주는 명령입니다.
왜 넣어야 하냐면, A.h라는 파일이 여러 곳에서 복잡하게 #include 되어 쓰이게 된다면 그때마다 각각 정의가 추가되게 되어 중첩되는 경우가 발생됩니다. 이 경우 중복 정의되었다는 에러가 발생하게 되지요. 즉 같은 내용이 여러번 빌드되게 되는겁니다. 이를 막기위해 #pragma once가 필요합니다. 물론 컴파일시간도 줄여주므로 대부분의 헤더파일에 추가하기를 추천합니다.
#pragma는 C언어에서 컴파일러에 직접 정보를 전하기 위해 사용합니다.
MSDN에서 발췌한 내용입니다.
'Programming > C++' 카테고리의 다른 글
seekg(0, ios::beg) doesn't work! (0) | 2010.11.28 |
---|---|
#define, #ifdef, #ifndef, #endif (0) | 2010.03.28 |
Header에서 #pragma once Vs #ifndef (0) | 2010.03.28 |
union(공용체)와 BitField (0) | 2010.03.14 |
extern 과 static 의미(지역변수/전역변수/정적변수) (0) | 2010.03.14 |