2023년 5월 29일 월요일

Include guard

When developing a source code as a team, it is easy to include header file repeatedly.

Include guard prevents header files from being included redundantly.

// Checks if _SOMETHING IF DECLARED
#ifndef _SOMETHING_
  
// Defines _SOMETHING_ if above
// conditions fails
#define _SOMETHING_

// define SOMETHING

#endif // _SOMETHING_

Or, you could use #pragma once in front of header file before you define somthing.

The difference between of them are the following.
#ifndef
If the header files are being included several times, the compiler checks every time whether something is defined or not.
It worsk for every compiler because it is preprocessor directive, that is independent for device.

#pragma once
The file that declared "#pragma once" will be compiled only once, and will not be even read again. It is faster than #ifndef. But, It is compiler directive and works for certain compiler. And It supports on or over Visual C++ 5.0 version.

댓글 없음:

댓글 쓰기