title: Should I include header file within a namespace?
body: <p>Currently I am working on a c framework and I want to embed a c++ package into it. However, there are lots of naming conflicts occurs. So I decided to to add a namespace to the C++ source. Now the question is should I move the #include "header.h" within namespace { } block? I just spent some time to figure out a bug which was resulted from the following codes.</p>
  
  <p>Original C++ source</p>
  
  <p>In a.h</p>
  
  <pre><code>#include &lt;unistd.h&gt;
  struct File 
  {
       void func(int fd);
  }; 
  </code></pre>
  
  <p>In a.cpp</p>
  
  <pre><code>#include "a.h"
  void File::func(int fd)
  {
      ::close( fd );
  }
  </code></pre>
  
  <p>And I added the namespace like this</p>
  
  <p>New a.h</p>
  
  <pre><code>namespace MyAddedNameSpace 
  { 
      #include &lt;unistd.h&gt;
      struct File 
      {
          void func(int fd);
      }; 
  }
  </code></pre>
  
  <p>New a.cpp</p>
  
  <pre><code>#include "a.h"
  namespace MyAddedNameSpace 
  {
      void File::func(int fd)
      {
          ::close( fd );
      }
  }
  </code></pre>
  
  <p>And compiler complains that ::close() has not been declared. </p>
  
  <p>The reason Why I put the #include directive inside namespace block is because the c++ package I imported also use #ifndef flag to include header files as follows. So I think the easy way is to put all codes within namespace block {}</p>
  
  <pre><code>#ifndef 
      #include &lt;header1.h&gt; 
      #include &lt;header2.h&gt;
      ...
  #else 
      #include &lt;header3.h&gt;
      #include &lt;header4.h&gt;
      ...
  #endif
  </code></pre>
  
  <p>Now I solved this problem by adding the extra line in cpp file</p>
  
  <pre><code>#include &lt;unistd.h&gt;  //new added line 
  #include "a.h"
  namespace MyNameSpace 
  {
      void File::func(int fd)
      {
          ::close( fd );
      }
  }
  </code></pre>
  
  <p>But I am not satisfied with this solution since unistd.h header has already been included in a.h, but inside the namespace MyAddedNameSpace, or should I added the prefix MyNameSpace to all the function calls where the compiler complain no such function declared ? </p>
  
  <p>Thanks for the reply.</p>