title: Pybind11 - Where to put PYBIND11_MODULE
body: <p>Im currently playing around with pybind11 a bit. Im trying to create a C++ class which then gets passed to a python interpreter embedded in my C++ source.</p>
  
  <p>I created some dummy class just to test the basic functionality I kept everything in a single source file. This approach compiled and ran without any problems.</p>
  
  <p>Now I separated my dummy Class <code>Test</code> into a Test.h and Test.cpp</p>
  
  <p><strong>Test.h</strong></p>
  
  <pre><code>#pragma once
  #include&lt;iostream&gt;
  #include"pybind11\pybind11.h"
  
  namespace py = pybind11;
  
  class Test
  {
  public:
      Test(const std::string &amp;s);
      ~Test();
  
      void printStr();
  
  private:
      std::string _s;
  };
  </code></pre>
  
  <p><strong>Test.cpp</strong></p>
  
  <pre><code>#include "Test.h"
  
  PYBIND11_MODULE(TestModule, m)
  {
      py::class_&lt;Test&gt;(m, "Test") 
          .def(py::init&lt;const std::string &amp;&gt;())
          .def("printStr", &amp;Test::printStr); 
  }
  
  
  Test::Test(const std::string &amp;s) : _s(s)
  {
  
  }
  Test::~Test()
  {
  
  }
  
  void Test::printStr()
  {
      std::cout &lt;&lt; "---&gt; " &lt;&lt; _s &lt;&lt; std::endl;
  }
  </code></pre>
  
  <p><strong>main.cpp</strong></p>
  
  <pre><code>#include"Test.h"
  
  int main(int argc, char **argv)
  {
  
      PyImport_AppendInittab("TestModule", PyInit_TestModule);
      Py_Initialize(); 
  
      PyRun_SimpleString("import TestModule");
      PyRun_SimpleString("t = TestModule.Test(\"str\")"); 
      PyRun_SimpleString("t.printStr()"); 
      Py_Finalize();
  
      getchar();
  
      return 1;
  }
  </code></pre>
  
  <p>After putting the Class <code>Test</code> into a new file the Compiler cannot find the <code>PyInit_TestModule</code> (main.cpp line: 6) anymore since this is generated by the <code>PYBIND11_MODULE</code> Macro which lives in the Test.cpp file(MSVS2017 Error: C2065).</p>
  
  <p>I tried putting the <code>PYBIND11_MODULE</code> Macro into the Test.h. This however resulted in a linker error which said that "_PyInit_TestModule" is already defined in main.obj (MSVS2017 Error: LNK2005)</p>
  
  <p>Putting the <code>PYBIND11_MODULE</code> Macro in the main.cpp file works.
  However I feel like this will become quite unreadable as soon as you put a lot of custom Module definitions into main.cpp or even worse you have multiple Python-Interpreter being started from different source files where you then 
  need to put the same definition in all those files which will be a mess and most likely turn into a linker error.</p>
  
  <p>Has one of you faced the same Problem and how did you solve it?</p>