From 637c4d5d302d7405fc7e85bce4cacdd4bb19edf8 Mon Sep 17 00:00:00 2001 From: Emil Williams Date: Sat, 20 Jul 2024 17:17:05 +0000 Subject: [PATCH] Define At Beginning Header Guard --- .gitignore | 6 ++++++ chad.tex | 8 +++++--- reasoning/header-guard/README.md | 8 ++++++++ reasoning/header-guard/a.h | 10 ++++++++++ reasoning/header-guard/ac.h | 10 ++++++++++ reasoning/header-guard/b.h | 10 ++++++++++ reasoning/header-guard/bc.h | 10 ++++++++++ reasoning/header-guard/main.c | 19 +++++++++++++++++++ 8 files changed, 78 insertions(+), 3 deletions(-) create mode 100644 .gitignore create mode 100644 reasoning/header-guard/README.md create mode 100644 reasoning/header-guard/a.h create mode 100644 reasoning/header-guard/ac.h create mode 100644 reasoning/header-guard/b.h create mode 100644 reasoning/header-guard/bc.h create mode 100644 reasoning/header-guard/main.c diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f126ada --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +auto +chad.pdf +chad.log +chad.aux +*.out +*.o diff --git a/chad.tex b/chad.tex index 3112654..c655fda 100644 --- a/chad.tex +++ b/chad.tex @@ -82,16 +82,18 @@ type a = { Do not use relative pathing in headers, instead use compiler flags or full paths to enable your compilation. -\bitem{Define At End Header Guard} \\ +\bitem{Define At Beginning Header Guard} \\ Use this kind of header guard: \begin{verbatim} #if HEADER_H -... #define HEADER_H -#endif +... +#endif // HEADER_H \end{verbatim} +\bitem{No Double Blank Line} Shortens code and removes blank redundancy, use Single Blank Lines as much you want. + \bitem{Namespacing For Externally Exposed Headers \& Variables} Add a namespace suffix for headers \& variables meant to be used outside your project. diff --git a/reasoning/header-guard/README.md b/reasoning/header-guard/README.md new file mode 100644 index 0000000..909ea03 --- /dev/null +++ b/reasoning/header-guard/README.md @@ -0,0 +1,8 @@ +## Justification for Define At End Header Guard -> Define At Beginning Header Guard. + +Define At End Header Guard is a very pretty style for Header Guards, however is nonviable due to: + +Define At End Header Guard fail to properly handle circular dependence as the define comes after the dependency, +hence using Define At Beginning Header Guard is always preferable. + +This was brought to light by an long-term Anonymous contributor, and is responsible for this change and justification. diff --git a/reasoning/header-guard/a.h b/reasoning/header-guard/a.h new file mode 100644 index 0000000..179fd6c --- /dev/null +++ b/reasoning/header-guard/a.h @@ -0,0 +1,10 @@ +#ifndef A_H + +#include "b.h" + +struct a { + int i; +}; + +#define A_H +#endif diff --git a/reasoning/header-guard/ac.h b/reasoning/header-guard/ac.h new file mode 100644 index 0000000..304fdda --- /dev/null +++ b/reasoning/header-guard/ac.h @@ -0,0 +1,10 @@ +#ifndef A_H +#define A_H + +#include "bc.h" + +struct a { + int i; +}; + +#endif diff --git a/reasoning/header-guard/b.h b/reasoning/header-guard/b.h new file mode 100644 index 0000000..8c75f68 --- /dev/null +++ b/reasoning/header-guard/b.h @@ -0,0 +1,10 @@ +#ifndef B_H + +#include "a.h" + +struct b { + int i; +}; + +#define B_H +#endif diff --git a/reasoning/header-guard/bc.h b/reasoning/header-guard/bc.h new file mode 100644 index 0000000..a1b2e23 --- /dev/null +++ b/reasoning/header-guard/bc.h @@ -0,0 +1,10 @@ +#ifndef B_H +#define B_H + +#include "ac.h" + +struct b { + int i; +}; + +#endif diff --git a/reasoning/header-guard/main.c b/reasoning/header-guard/main.c new file mode 100644 index 0000000..d468a94 --- /dev/null +++ b/reasoning/header-guard/main.c @@ -0,0 +1,19 @@ +// @BAKE gcc $@ -o $*.out $+ + +#ifndef CIRCULAR +# define CIRCULAR 0 +#endif + +#if CIRCULAR +// Define At End Header Guard +# include "a.h" +# include "b.h" +#else +// Define At Beginning Header Guard +# include "ac.h" +# include "bc.h" +#endif + +signed main () { + return(0); +}