title: Anonymous namespace causes undefined reference here - works there
body: <p>I've written some C++ code for an embedded system which works like a charm. The current task is to emulate the behaviour of this device on a PC. Some of the code has to be ported: For a first test I'm using mingw (g++) while the Embedded system is an STM32 and uses the KEIL µVision toolchain.</p>
  
  <p>I've run into a problem that is not really related to functional behaviour rather than a compiler specific weirdness. I have 2 classes defined in an anonymous namespace because they are included throughout the whole project. Now on the embedded device this compiles and runs without a problem. g++ complains about an undefined reference!</p>
  
  <p>When I remove the anonymous namespace arround the class it compiles and runs! But why? Here is some example code that reproduces the situation:
  main.cpp:</p>
  
  <pre><code>#include "notmain.h"
  #include "theclass.h"
  
  A *ourA=NULL;
  
  int main()
  {
      theA = new A();
      theA-&gt;dostuff(1024);
      sunshine sun;
      sun.Init();
  }
  </code></pre>
  
  <p>notmain.cpp:</p>
  
  <pre><code>#include "notmain.h"
  #include "theclass.h"
  
  void sunshine::Init()
  {
      theA-&gt;dostuff(127);
  }
  </code></pre>
  
  <p>notmain.h:</p>
  
  <pre><code>#ifndef NOTMAIN_H_
  #define NOTMAIN_H_
  class sunshine
  {
  public:
      void Init();
  };
  #endif
  </code></pre>
  
  <p>theclass.h:</p>
  
  <pre><code>#ifndef THECLASS_H_
  #define THECLASS_H_
  #include &lt;stdio.h&gt;
  #define theA ourA
  namespace
  {
      class A
      {
      public:
          void dostuff(int b)
          {
              a = b;
              printf("Hello: %d\n",a);
          }
      private:
          int a;
      };
  }
  extern A *ourA;
  #endif
  </code></pre>
  
  <p>Compiler/Linker Output:
      09:09:57 <strong>** Incremental Build of configuration Debug for project Testo **</strong>
      Info: Internal Builder is used for build
      g++ -O0 -g3 -Wall -c -fmessage-length=0 -o main.o "..\main.cpp" 
      g++ -O0 -g3 -Wall -c -fmessage-length=0 -o notmain.o "..\notmain.cpp" 
      g++ -o Testo.exe notmain.o main.o 
      notmain.o: In function <code>ZN8sunshine4InitEv':
      D:\Projekte\Testo\Debug/../notmain.cpp:6: undefined reference to</code>ourA'
      collect2.exe: error: ld returned 1 exit status</p>
  
  <pre><code>09:09:57 Build Finished (took 702ms)
  </code></pre>
  
  <p>Removing that namespace fixes the problem but why does it compile, link, work in KEIL? Can anyone explain this to me?</p>