Wrapping C++ class with SWIG, Mac OS X

Paul Anton Letnes paul.anton.letnes at gmail.com
Fri Apr 11 16:18:35 EDT 2008


Hello guys,


(related to previous thread on wrapping C/C++ in Python, trying the  
SWIG approach.)
Trying to map a C++ class to python, one method for now. Running the  
following commands to "compile":

--------------------------------------
#!/usr/bin/env bash

MOD_NAME=Wavelet


swig -c++ -python -o ${MOD_NAME}_wrap.cpp ${MOD_NAME}.i

gcc -c++ -fPIC -c ${MOD_NAME}.cpp -o ${MOD_NAME}.o -I/usr/include/ 
python2.5 -I/usr/lib/python2.5

gcc -c++ -fPIC -c ${MOD_NAME}_wrap.cpp -o ${MOD_NAME}_wrap.o -I/usr/ 
include/python2.5 -I/usr/lib/python2.5

gcc -bundle -flat_namespace -undefined suppress -o _${MOD_NAME}.so $ 
{MOD_NAME}.o  ${MOD_NAME}_wrap.o
--------------------------------------

The source code is:
--------------------------------------
Wavelet.h
--------------------------------------
#ifndef _WAVELET_H_
#define _WAVELET_H_

#include <iostream>
#include <vector>

using namespace std;

class Wavelet
{

public:
     Wavelet(vector<double> theV);
     ~Wavelet();
     vector<double> GetDaub4Trans();

private:
     vector<double> v;

};


#endif /*_WAVELET_H_*/
--------------------------------------
and Wavelet.cpp:
--------------------------------------
#include "wavelet.h"

Wavelet::Wavelet(vector<double> theV)
{
     this->v = theV;
}

Wavelet::~Wavelet()
{
     // Nothing for now
}

vector<double> Wavelet::GetDaub4Trans()
{
     vector<double> retV = vector<double>();
     retV.push_back(3.14);
     retV.push_back(2.71);
     retV.push_back(1.62);
     return retV;
	// just to test the approach - everything in here I can fix later.
}
--------------------------------------
This seems to compile, but in python I get:
--------------------------------------
$ python
imPython 2.5.2 (r252:60911, Mar 30 2008, 22:49:33)
[GCC 4.0.1 (Apple Inc. build 5465)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
 >>> import Wavelet
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
   File "Wavelet.py", line 7, in <module>
     import _Wavelet
ImportError: dlopen(./_Wavelet.so, 2): Symbol not found:  
__ZNKSt11logic_error4whatEv
   Referenced from: /Users/paul/Desktop/Wavelet_SWIG_Cpp/_Wavelet.so
   Expected in: flat namespace

 >>>
--------------------------------------

Any ideas or tips? SWIG seems very nice for simple C methods where you  
pass an int and return an int, but I can't seem to figure out the  
syntaxes etc for more complicated stuff - arrays, vector<T>, C++, ...  
Appreciate any help!


Cheers,
Paul.



More information about the Python-list mailing list