title: SWIG + setup.py: ImportError: dynamic module does not define init function (init_foo)
body: <p>I am trying to wrap a function foo in <code>test.cpp</code> with swig. I have a header <code>foo.h</code> which contains the declaration of the function foo. <code>test.cpp</code> is dependent upon a external header <code>ex.h</code> and shared object file <code>libex.so</code> located in <code>/usr/lib64</code> </p>
  
  <p>I followed the <a href="http://bitsofpy.blogspot.com/2009/05/swig-with-c-and-external-libs.html">blog post from here</a>.</p>
  
  <p>I am able to build the module with <code>python setup.py build_ext --inplace</code>. However when I try to import it I get the following error and I am not sure what I am missing as most others questions with this error do not use a <code>setup.py</code> file. Below is an example of what I currently have.</p>
  
  <p><strong>The Error on importing _foo:</strong></p>
  
  <pre><code>&gt;&gt;&gt; import _foo
  
  ImportError: dynamic module does not define init function (init_foo)
  </code></pre>
  
  <p><strong>test.i</strong></p>
  
  <pre><code>%module foo
  
  
  %{
  #pragma warning(disable : 4996)
  #define SWIG_FILE_WITH_INIT
  #include "test.h"
  %}
  
  %include &lt;std_vector.i&gt;
  %include &lt;std_string.i&gt;
  %include "test.h"
  </code></pre>
  
  <p><strong>test.cpp</strong></p>
  
  <pre><code>#include "ex.h"
  
  void foo(int i){
      return;
  };
  </code></pre>
  
  <p><strong>test.h</strong></p>
  
  <pre><code>#include "ex.h"
  
  void foo(int i);
  </code></pre>
  
  <p><strong>setup.py</strong></p>
  
  <pre><code>try:
      from setuptools.command.build_ext import build_ext
      from setuptools import setup, Extension, Command
  except:
      from distutils.command.build_ext import build_ext
      from distutils import setup, Extension, Command
  
  foo_module = Extension('_foo', 
                          sources=['foo.i' , 'foo.cpp'],
                          swig_opts=['-c++'],
                          library_dirs=['/usr/lib64'],
                          libraries=['ex'],
                          include_dirs = ['/usr/include'],
                          extra_compile_args = ['-DNDEBUG', '-DUNIX', '-D__UNIX',  '-m64', '-fPIC', '-O2', '-w', '-fmessage-length=0'])
  
  setup(name='mymodule',
        ext_modules=[foo_module],
        py_modules=["foo"],
        )
  </code></pre>