본문으로 바로가기

pragma 의 의미를 action 으로 많이들 알고 계시는데, 인터넷에서 알아보니, 사전적인 의미는 "만능" 이었습니다.

어원설명 원문입니다.
아래를 참고하세요.

A pragma (from the Greek word meaning action) is used to direct the actions of the compiler in particular ways, but has no effect on the semantics of a program (in general).

Pragmas are used to control listing, to define an object configuration (for example, the size of memory), to control features of the code generated (for example, the degree of
optimization or the level of diagnostics), and so on.

Such directives are not likely to be related to the rest of the language in an obvious way. Hence the form taken should not intrude upon the language, but it should be uniform.

Thus, the general form of pragmas is defined by the language.
They start with the reserved word pragma followed by a pragma
identifier, optionally followed by a list of arguments enclosed by parentheses, and terminated by a semicolon.

The overall syntax of the pragma identifier and arguments is similar to that of a procedure call. Pragmas are allowed at places where a declaration or a statement is allowed; also at places where other constructs that play the role of declarations (for example clauses) are allowed.


pragma 는 컴파일에게 그 뒤에오는 내용에 따라 어떤일을 하라는 전처리명령입니다.
따라서, 앞에 # 과 같이 사용되죠. 몇가지 자주 사용되는것을 설명하자면,

#pragma once
 한번 컴파일 되면 더 이상 컴파일 하지 않는다는 뜻입니다.
 여러개의 cpp파일이 있을때, 하나의 cpp화일이 수정되면, 그 화일만 컴파일하고 나머지는 하지말아란 뜻이죠.

#pragma comment( lib, "ws2_32" )
이것은, 컴파일시 ws2_32.lib 파일을 링크하라는 명령입니다.
보통 Visual studio같은 IDE 개발환경에서는, 프로젝트 셋팅에서 해주지만, 혹 그런부분을 빼먹거나 환경이 바뀔때를 대비해서 이렇게 해두면 편하죠.

#pragma pack(n)
위에서, n의 값으로, 1,2,4,8등이 올수 있으며, 특히 네트웍통신쪽을 개발할때 구조체의 멤버들 align할때 사용하는 것으로서, 빈번하게 사용됩니다.
구조체 정렬부분은 중요하지만, 여기서는 그쪽까지 언급하기에는 양이 많아서 여기까지만 설명함.

#pragma warning
컴파일시에 어떤 조건(#if, #ifndef)에의해 개발자에게 어떤것을 알려주고 싶을 경우 사용.


pragma에 대한 더 자세한 설명은 각 컴파일러 헬프를 보시면 완벽하겠죠. ^^