C++Builder Programming Forum
C++Builder  |  Delphi  |  FireMonkey  |  C/C++  |  Free Pascal  |  Firebird
볼랜드포럼 BorlandForum
 경고! 게시물 작성자의 사전 허락없는 메일주소 추출행위 절대 금지
C++빌더 포럼
Q & A
FAQ
팁&트릭
강좌/문서
자료실
컴포넌트/라이브러리
메신저 프로젝트
볼랜드포럼 홈
헤드라인 뉴스
IT 뉴스
공지사항
자유게시판
해피 브레이크
공동 프로젝트
구인/구직
회원 장터
건의사항
운영진 게시판
회원 메뉴
북마크
볼랜드포럼 광고 모집

C++빌더 팁&트릭
C++Builder Programming Tip&Tricks
[926] C++Builder2010 헤더 중복 방지 지원
크레브 [kkol] 15499 읽음    2009-11-09 16:05
C++ 2010에서 헤더가 중복 Include 되는 것을 막기위해서 사용하던 #ifndef/define/endif 3개의 조합대신에
간단하게 사용할 수 있는 #pragma once 를 지원합니다.

C++Builder 2007 HELP에는 안나오는것으로 봐서
2009나 2010부터 지원하는듯 합니다.

Visual C++에서는 전부터 지원했던것 같습니다.


[기존 방법]

#ifndef  XXXXXXX
#define XXXXXXX

#endif



[간단한 방법]
#pragma once



즉 헤더파일 제일 위에 간단히 #pragma once 만 추가하면됩니다.


C++도움말 긁어온것

Syntax

#pragma once
Description

#pragma once is a preprocessor directive used to prevent header files from being included multiple times. The #pragma once directive, once present in a file, assures that the file will not be included multiple times in the current project.


An example of why #pragma once is useful is the fact that a file that includes another header is sufficient. For a logical example where the directive can be used, see the code below.

Vertex.h
#pragma once
struct Vertex{
  int x_coord;
  int y_coord;
  int z_coord;
};




Triangle.h

#include "Vertex.h"
struct Triangle{
  Vertex p1;
  Vertex p2;
  Vertex p3;
};

Square.h
struct Square{
  Vertex v1;
  Vertex v2;
  Vertex v3;
  Vertex v4;
};



Drawing.cpp

#include 
#include "Triangle.h"
#include "Vertex.h"
int main()
{
  //a triangle
  Triangle tr0;
  //a square
  Square sq0;
  return 0;
}



Because both the triangle and the square need the vertex struct to know their position in three-dimensional space, both of them include Vertex.h. The triangle and the square are included too in the main .cpp, therefore the Vertex.h file would be included twice (by Triangle.h and by Square.h). Using #pragma once in Vertex.h prevents the preprocessor from including the file a second time. As a result, no error is thrown.

Note: The same thing can be achieved with #ifndef/#define/#endif, which is the ANSI standard way of dealing with the double inclusion problem.
망치 [mangchy]   2009-11-09 16:23 X
어떤 헤더보면
#if _MSC_VER > 1000
#pragma once
#endif
이렇게 구현되어 있는걸로 봐서는 빌더 4.0부터 지원되는거 같습니다.
망치 [mangchy]   2009-11-09 16:24 X
아 4.0이상버젼부터겠군요...
크레브 [kkol]   2009-11-09 16:38 X
테스트 해 본결과 C++Builder 6.0 , 2007에서는 지원하지 않습니다.
2009는 없어서 테스트 못하고
2010에서는 지원합니다.
박지훈.임프 [cbuilder]   2009-11-09 16:51 X
#pragma once는 C++빌더 2010에서 추가된 기능입니다.
망치 [mangchy]   2009-11-09 17:31 X
아 그렇군요...그럼 위에 헤더에서는 그냥 패스했겠군...흠...
_MSC_VER이라는것은 Visual C++ 버젼을 이야기 하는거였군요.
박지훈.임프 [cbuilder]   2009-11-09 19:02 X
C++빌더에 포함된 Win32 SDK 헤더는 마이크로소프트에서 제공한 그대로이기 때문에 그런 내용이 들어있는 거랍니다.
당연히 지금까지는 C++빌더에서는 동작하지 않았었고, C++빌더 2010부터 동작합니다.

+ -

관련 글 리스트
926 C++Builder2010 헤더 중복 방지 지원 크레브 15499 2009/11/09
Google
Copyright © 1999-2015, borlandforum.com. All right reserved.