title: Using "extern C" from C++ with vector for ctypes
body: <p>My goal is to use ctypes to call from within Python a C++ library I'm creating separately, where I am passing numpy vectors to the C++ routines via pointers. So Python would allocate the memory and just pass the address, and then the C++ routine would access the data and perform its calculations and return the results.</p>
  
  <p>Since I'm new to ctypes, right now I'm gradually building a working toy example. I'm starting off writing the C++ code and creating a C extern interface that the Python wrapper code will use. I haven't even started on the Python wrapper code yet but have some experience with that aspect already. </p>
  
  <p>Here is my example which includes a constructor and a simple total function.</p>
  
  <pre><code>// foo.cpp //
  #include &lt;iostream&gt;
  #include &lt;string&gt;
  #include &lt;vector&gt;
  using namespace std;
  
  class Data {
  public:
    Data(vector&lt;double&gt;*);
    void PrintTotal(void);
  private:
    vector&lt;double&gt;* myContents;
  };
  Data::Data(vector&lt;double&gt;* input) {
    std::cout &lt;&lt; "Entered constructor in C++" &lt;&lt; std::endl;
    myContents = input;
  }
  void Data::PrintTotal(void) {
    double total = 0;
    for (int i=0; i&lt;myContents-&gt;size(); i++){
      total += (*myContents)[i];
    }
    std::cout &lt;&lt; "Hello, my total is " &lt;&lt; total &lt;&lt; std::endl;
  }
  
  extern "C" {
    Data* Data_new(double (**input)) {return new Data(input);}
    void Print_Total(Data* pData) {pData-&gt;PrintTotal();}
  }
  </code></pre>
  
  <p>Note in particular that I'm using the vector class from the STL, which may be part of the problem described below. The idea is that the class <code>Data</code> holds a pointer to the data and does not replicate the data.</p>
  
  <p>When I try to compile this using the command</p>
  
  <pre><code>g++ -c -fPIC foo.cpp -o foo.o
  </code></pre>
  
  <p>on our Linux machine, I get the following error:</p>
  
  <pre><code>foo.cpp: In function âData* Data_new(double**)â:
  foo.cpp:26: error: no matching function for call to âData::Data(double**&amp;)â
  foo.cpp:13: note: candidates are: Data::Data(std::vector&lt;double, std::allocator&lt;double&gt; &gt;*)
  foo.cpp:6: note:                 Data::Data(const Data&amp;)
  </code></pre>
  
  <p>This seems pretty clear to me: it's saying that the way I call the Data constructor in the third to last line (#26) in <code>foo.cpp</code> does not coincide with the constructor I wrote in the C++ part of the code. In both the C++ code and the C extern code, I'm trying to say that the input is a pointer to a vector/array of doubles. I've tried other options on line #36 but it still doesn't compile.</p>
  
  <p>How do I write the extern C part while still using the <code>vector</code> class (which I will find useful when I code the real algorithm) in the C++ part? Am I running into trouble because <code>vector</code> is part of the STL, which doesn't work well with extern C?</p>
  
  <p>Thanks in advance.</p>