Header File Rules

Header files are the public-facing contracts of your components. These rules ensure they are well-formed, protected against common issues, and contain necessary legal information.

Pragma Once

HEADER_PRAGMA_ONCE

Requires every header file to start with `#pragma once` to prevent multiple inclusions, which can lead to compilation errors. It is a more modern and efficient alternative to traditional include guards.

Examples:

✅ Correct
❌ Incorrect (Lint Error)
// my_class.h
#pragma once

#include <string>

class MyClass {
public:
    MyClass(std::string name);
private:
    std::string name_;
};
// my_class.h
// Missing #pragma once

#include <string>

class MyClass {
public:
    MyClass(std::string name);
private:
    std::string name_;
};

// Also bad: using old-style include guards
#ifndef MY_CLASS_H
#define MY_CLASS_H
// ... content ...
#endif // MY_CLASS_H

Summary

Well-structured header files are fundamental to a C++ project:

  • #pragma once: The modern, standard way to prevent multiple inclusions.