Organize code to minimize header dependencies and improve compilation times. Use forward declarations instead of includes when possible, avoid unnecessary dependencies, and follow proper header organization patterns.
Organize code to minimize header dependencies and improve compilation times. Use forward declarations instead of includes when possible, avoid unnecessary dependencies, and follow proper header organization patterns.
Key practices:
static
declarations in header files as they create separate instances in each translation unitExample:
// Good: Use forward declaration in header
// MyClass.h
class WarnUnusedResultAttr; // Forward declaration
class MyClass {
WarnUnusedResultAttr* attr;
};
// MyClass.cpp - actual include in implementation
#include "clang/AST/Attr.h"
// Bad: Heavy include in header
// MyClass.h
#include "clang/AST/Attr.h" // Unnecessary dependency
This approach reduces compilation times, minimizes rebuild cascades when headers change, and creates cleaner module boundaries.
Enter the URL of a public GitHub repository