title: should pragma once be on top of every header as default
body: <p>I'm learning C++ and I have encountered (and fix) what seems to be a very classic problem :</p>
  <pre><code>g++ main.cpp A.cpp B.cpp -o out
  In file included from B.h:1,
                   from main.cpp:3:
  A.h:1:7: error: redefinition of ‘class A’
      1 | class A {
        |       ^
  In file included from main.cpp:2:
  </code></pre>
  <p>Which from a quick research (assuming I understood it correctly), happens because the <a href="https://en.wikipedia.org/wiki/Include_guard" rel="nofollow noreferrer">#include operation is not &quot;idempotent&quot;</a> (a term I discovered with this problem).</p>
  <p>To illustrate my question I propose a minimal working example.</p>
  <blockquote>
  <p>main.cpp</p>
  <pre><code>#include &quot;A.h&quot;
  #include &quot;B.h&quot;
  #include &lt;iostream&gt;
  
  int main () {
    std::cout &lt;&lt; &quot;Hello world&quot; &lt;&lt; std::endl;
    A a;
    B b(a);
    return 0;
  }
  </code></pre>
  </blockquote>
  <blockquote>
  <p>A.h</p>
  <pre><code>#include &lt;iostream&gt;
  
  class A {
    public:
        void test();
  };
  </code></pre>
  <p>A.cpp</p>
  <pre><code>#include &quot;A.h&quot;
  
  void A::test () {
    std::cout &lt;&lt; &quot;test&quot; &lt;&lt; std::endl;
  }
  </code></pre>
  </blockquote>
  <blockquote>
  <p>B.h</p>
  <pre><code>#include &quot;A.h&quot;
  
  class B {
    public:
        B(A);
  };
  </code></pre>
  <p>B.cpp</p>
  <pre><code>#include &quot;B.h&quot;
  #include &lt;iostream&gt;
  
  B::B(A a){
    a.test();
  }
  </code></pre>
  </blockquote>
  <p>Compiling the program with <code>g++ main.cpp A.cpp B.cpp</code> or more specifically <code>g++ -c main.cpp</code> will fail with the error shown above.</p>
  <p>I understand that the compiler <a href="https://en.wikipedia.org/wiki/Transclusion" rel="nofollow noreferrer">transcludes</a> the header of &quot;A&quot; twice when compiling main.cpp (once at main.cpp:1 and once again at B.h:1 during its own inclusing at main.cpp:2). Effectively, the compiler 'sees' the definition of <code>class A</code> twice and thinks we are defining A twice.</p>
  <p>What I Fail to understand is the include guards:</p>
  <p>To fix this, one may use the keyword: <code>pragma once</code> at the top of the file that is included more than once as such:</p>
  <blockquote>
  <p>A.h fixed with #pragma once</p>
  <pre><code>#pragma once
  #include &lt;iostream&gt;
  
  class A {
    public:
        void test();
  };
  </code></pre>
  </blockquote>
  <p>Allowing the program to compile nicely.</p>
  <p><strong>To me this suggest that I should start every header with #pragma once !!!</strong> Which can't be right is it? Is it common practice? If so, Is there a way to do that at compile time instead (as a flag for instance)?</p>
  <p>If I don't, I may not use object A as member of class A nor pass it as argument to B (as in the constructor of B in my example) if such A and B could be used individually in another file; unless I add <code>#pragma once</code> reactively every time the problem pops up which seems &quot;dirty&quot; to me. Furthermore, I would not be able to share my sources with anyone in fear they encounter the situation with two of my objects without having to add <code>pragma once</code> in my files themselves.</p>
  <p>What Am I missing? Is there a way to avoid the problem altogether?</p>