title: C++ - Python Binding with ctypes - Return multiple values in function
body: <p>I found this example of C++ Python binding: 
  <a href="https://stackoverflow.com/questions/145270/calling-c-c-from-python">Calling C/C++ from python?</a>
  According to the answer there, I created some testfiles:</p>
  
  <p>foo.cpp:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;utility&gt;
  
  
  int bar_2(int a, int b){
      return a*b;
  }
  
  std::pair&lt;int, int&gt; divide(int dividend, int divisor)
  {
     return std::make_pair(dividend / divisor, dividend % divisor);
  }
  
  extern "C" {
      int bar_2_py(int a, int b){ return bar_2(a,b); }
      std::pair&lt;int, int&gt; divide_py(int d, int div){return divide(d,div);}
  }
  </code></pre>
  
  <p>fooWrapper.py:</p>
  
  <pre><code>#!/usr/bin/env python
  
  from ctypes import cdll
  lib = cdll.LoadLibrary('./libfoo.so')
  
  def bar_2(a, b):
      res = lib.bar_2_py( a,b )
      return res
  
  def divide(d,div):
      res = lib.divide_py(d,div)
      return res
  </code></pre>
  
  <p>Then </p>
  
  <pre><code>g++ -c -fPIC foo.cpp -o foo.o
  g++ -shared -Wl,-soname,libfoo.so -o libfoo.so  foo.o
  </code></pre>
  
  <p>creates libfoo.so</p>
  
  <p>If I import it and run the functions in iPython I get the right value for function "bar_2" but a (partly) wrong answer for "divide":</p>
  
  <pre><code>from fooWrapper import bar_2, divide
  bar_2(10,2) # returns 20, which is right
  divide(10,3) # returns 3
  </code></pre>
  
  <p>Obviously, the return-value is right for the first value of the pair (as 10/3 int division is 3). But the second value was getting lost. </p>
  
  <p>So, what would be best practice to obtain multiple values (in this case 2 integer values)? </p>
  
  <p>Thank you!</p>