title: Wrapping a C++ file as a Python file using SWIG
body: <p>I'm new to using SWIG and a bit out of my depth as a programmer. I would like to be able to call the functions of a C++ class in python 2 by importing the wrapped class as a module 'import C++_file' and then call it within my python class with something like 'C++_file.function(inputs)'.</p>
  
  <p>Following <a href="http://intermediate-and-advanced-software-carpentry.readthedocs.io/en/latest/c++-wrapping.html" rel="nofollow noreferrer">http://intermediate-and-advanced-software-carpentry.readthedocs.io/en/latest/c++-wrapping.html</a>, I am wrapping the header file multiplyChannel.h:</p>
  
  <pre><code>#include &lt;vector&gt;
  #include &lt;complex&gt;
  
  using namespace std;
  
  class MultiplyChannel {
      public:
          MultiplyChannel(double in_alpha);
          void applyToVector(vector&lt;complex&lt;double&gt; &gt; *in_signal);
      private:
          double alpha;
  };
  </code></pre>
  
  <p>which corresponds to my example C++ file multiplyChannel.cpp:</p>
  
  <pre><code>#include "multiplyChannel.h"
  #include &lt;vector&gt;
  #include &lt;complex&gt;
  
  using namespace std;
  
  MultiplyChannel::MultiplyChannel(double in_alpha){
      this-&gt;alpha = in_alpha;
  }
  
  void MultiplyChannel::applyToVector(vector&lt;complex&lt;double&gt; &gt; *in_signal){
      unsigned int size = in_signal-&gt;size();
      for (int i = 0; i &lt; size; i++) {
          in_signal-&gt;at(i).real() = in_signal-&gt;at(i).real() * this-&gt;alpha;
          in_signal-&gt;at(i).imag() = in_signal-&gt;at(i).imag() * this-&gt;alpha;
      }
  }
  </code></pre>
  
  <p>With the makefile:</p>
  
  <pre><code>all:
      swig -python -c++ -o mult.cpp swigdemo.i
      python setup.py build_ext --inplace
  </code></pre>
  
  <p>the wrapper file swigdemo.i:</p>
  
  <pre><code>%module swigdemo
  
  %{
  #include &lt;stdlib.h&gt;
  #include "multiplyChannel.h"
  %}
  
  %include "multiplyChannel.h"
  </code></pre>
  
  <p>and setup.py build file:</p>
  
  <pre><code>from distutils.core import setup, Extension
  
  extension_mod = Extension("_swigdemo", ["mult.cpp"])
  
  setup(name = "swigdemo", ext_modules=[extension_mod])
  </code></pre>
  
  <p>by typing in my Ubuntu command window:</p>
  
  <pre><code>$ make
  swig -python -c++ -o multiplyChannel.cpp swigdemo.i
  python setup.py build_ext --inplace
  running build_ext
  $ python setup.py build
  running build
  running build_ext
  </code></pre>
  
  <p>Testing the import using C++_tester.py, I try to multiply the vector [1, 2, 3] into [5, 10, 15] using the MultiplyChannel object 'demo' with instance variable 'in_alpha' of 5x, multiplying all inputs by 5:</p>
  
  <pre><code>#!/usr/bin/python
  
  import swigdemo
  
  if __name__ == '__main__':
      demo = swigdemo.MultiplyChannel(in_alpha=5)
      out = demo.applyToVector(in_signal=[1, 2, 3])
      print(out)
  </code></pre>
  
  <p>I am not getting past even the import line, receiving the following error:</p>
  
  <pre><code>$ python C++_tester.py
  ImportError: ./_swigdemo.so: undefined symbol: _ZN15MultiplyChannelC1Ed
  </code></pre>
  
  <p>And am unsure what to do as I cannot even gedit or vim into the .so file. I'm guessing my error lies in wrapping incorrectly in my wrapper, build, or makefile, as pretty much everything in the C++_tester.py file auto-completed in my Pycharm IDE.</p>
  
  <p>Many thanks!</p>