글 작성자: astrocosmos
 

pragma once - Wikipedia

Preprocessor directive in C and C++ In the C and C++ programming languages, pragma once is a non-standard but widely supported preprocessor directive designed to cause the current source file to be included only once in a single compilation.[1] Thus, #prag

en.wikipedia.org

In the C and C++ programming languages, #pragma once is a non-standard but widely supported preprocessor directive designed to cause the current source file to be included only once in a single compilation.

Thus, #pragma once serves the same purpose as include guards, but with several advantages, including:

  • less code
  • avoidance of name clashes
  • sometimes improvement in compilation speed

보통 내가 많이 사용했던 건 아래와 같은 #ifndef, #define, #endif 였다.

#ifndef GRANDPARENT_H
#define GRANDPARENT_H

struct foo {
    int member;
};

#endif /* GRANDPARENT_H */

같은 기능을 아래와 같이 #pragma once로 대신할 수 있다. 대부분의 C preprocessor에서 지원한다고 하지만 지원하지 않으면 위의 방법을 사용해야 한다.

#pragma once

struct foo {
    int member;
};
728x90