Pepe Barbe | 1 Jun 07:14 2006
Picon

Help wrapping C++ class [n00b]

Hello everyone,

As you might have seen I am complete n00b using SWIG and I am having
some problems. I've gone rapidly over the documentation but is just a
lot to bear in mind and I feel dizzy already.

My intent is to wrap a C++ to make it accessible in Python. But, I
think this class does not have the best interfaces to pass data
between the C++ code and Python (I use vectors, vectors of strings and
multidimensional vectors).

I would appreciate if anybody could give me pointers on how to
properly interface this class with Python. Below I am including the
declaration of the class, I think it should be enough but if you need
any other information, please let me know.

I've tested the class, so it seems that all the C++ code works
(marginally at least).

Thanks,
Pepe

#ifndef PYSASK_H_
#define PYSASK_H_

#include <string>
#include <vector>
#include <iostream>
#include "kernelmatrix.h"
#include "StringKernel.h"
(Continue reading)

Nitro | 1 Jun 10:49 2006

Re: Help wrapping C++ class [n00b]

> #ifndef PYSASK_H_
> #define PYSASK_H_
>
> #include <string>
> #include <vector>
> #include <iostream>
> #include "kernelmatrix.h"
> #include "StringKernel.h"
> #include "DataType.h"
> #include "kernelrow.h"
>
> using namespace std;
>
> class PySask
> {
> public:
> 	vector < vector <Real> > *kernel;
> 	vector <Real> *row;
> 	PySask();
> 	PySask(const int &input_size, const vector<string> &input_strings);
> 	//~PySask();
> 	int genKernel(const K_type &k_type, const int &k_param);
> 	int genRow(const string &data_string,const K_type &k_type, const int
> &k_param );
> 	void Initialize(const int &train_size, const vector<string>  
> &input_strings);
> private:
> 	int data_size;
> 	const vector <string> *kernel_strings;
> 	vector <Real> *diagonal;
(Continue reading)

Nitro | 1 Jun 15:34 2006

Re: Help wrapping C++ class [n00b]

Am Thu, 01 Jun 2006 14:54:17 +0200 hat Pepe Barbe <elventear <at> gmail.com>  
geschrieben:

> On 6/1/06, Nitro <nitro <at> dr-code.org> wrote:
>> Why don't you just try to wrap it? This class is actually fairly easy to
>> wrap. Be sure that you include the stl interfaces like std_string.i and
>> use some %template to create the vector<Real> and vector of vector. If  
>> you
>> encounter specific problems, please post again here and we will be glad  
>> to
>> help you.
>
> I previously had sent a message to the list with the problems I had
> compiling what I tried to wrap but it got rejected because it was too
> long. Below you'll find an excerpt of what I was trying to build and
> all the errors. There are so many that I don't know even where to
> start.
>
> Pepe

The wrapper that swig produced is c++. Your wrap file extension is .c, so  
gcc thinks you're trying to compile a C file. Try changing the extension  
to .cpp or .cxx.

-------------------------------------------------------
All the advantages of Linux Managed Hosting--Without the Cost and Risk!
Fully trained technicians. The highest number of Red Hat certifications in
the hosting industry. Fanatical Support. Click to learn more
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=107521&bid=248729&dat=121642
(Continue reading)

Pepe Barbe | 1 Jun 19:10 2006
Picon

Re: Help wrapping C++ class [n00b]

On 6/1/06, Nitro <nitro <at> dr-code.org> wrote:
> The wrapper that swig produced is c++. Your wrap file extension is .c, so
> gcc thinks you're trying to compile a C file. Try changing the extension
> to .cpp or .cxx.

Thanks for noticing. I've just realized that working with Python
distutils is no that straightforward. It seems that it doesn't have a
good support for C++ ([1],[2]). There seems to be a patch in [3] that
I haven't tested it.

I haven't found a solid resource with information on what is needed to
build the extension on MacOSX (gcc flags and all that), that Is why I
preferred Distutils. I would appreciate if anyone could point me to
some resources that could have that info.

Thanks,
Pepe

[1]. http://mail.python.org/pipermail/distutils-sig/2005-November/005387.html
[2]. http://mail.python.org/pipermail/distutils-sig/2005-May/004499.html
[3]. https://sourceforge.net/tracker/?func=detail&atid=305470&aid=1479255&group_id=5470

-------------------------------------------------------
All the advantages of Linux Managed Hosting--Without the Cost and Risk!
Fully trained technicians. The highest number of Red Hat certifications in
the hosting industry. Fanatical Support. Click to learn more
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=107521&bid=248729&dat=121642
Bill Spotz | 1 Jun 20:39 2006
Picon

Re: Help wrapping C++ class [n00b]

Pepe,

I develop python wrappers for a large C++ project, and I do most of  
my development on a Mac.  If you have questions regarding specific  
problems, I'd be happy to help offline.

My project uses make and autotools, so I have make call swig to  
generate the wrapper code and then I have make call setup.py to  
access distutils and build the extension module.  I found direct  
support for swig in distutils to be insufficiently developed (way  
back whenever it was that I looked into it).

On Jun 1, 2006, at 11:10 AM, Pepe Barbe wrote:

> On 6/1/06, Nitro <nitro <at> dr-code.org> wrote:
>> The wrapper that swig produced is c++. Your wrap file extension  
>> is .c, so
>> gcc thinks you're trying to compile a C file. Try changing the  
>> extension
>> to .cpp or .cxx.
>
> Thanks for noticing. I've just realized that working with Python
> distutils is no that straightforward. It seems that it doesn't have a
> good support for C++ ([1],[2]). There seems to be a patch in [3] that
> I haven't tested it.
>
> I haven't found a solid resource with information on what is needed to
> build the extension on MacOSX (gcc flags and all that), that Is why I
> preferred Distutils. I would appreciate if anyone could point me to
> some resources that could have that info.
(Continue reading)

Sohail Somani | 1 Jun 21:12 2006

RE: Help wrapping C++ class [n00b]

> -----Original Message-----
> From: swig-user-admin <at> lists.sourceforge.net 
> [mailto:swig-user-admin <at> lists.sourceforge.net] On Behalf Of Pepe Barbe
> Sent: Thursday, June 01, 2006 10:11 AM
> To: Nitro
> Cc: swig-user <at> lists.sourceforge.net
> Subject: Re: [Swig-user] Help wrapping C++ class [n00b]
> 
> On 6/1/06, Nitro <nitro <at> dr-code.org> wrote:
> > The wrapper that swig produced is c++. Your wrap file 
> extension is .c, so
> > gcc thinks you're trying to compile a C file. Try changing 
> the extension
> > to .cpp or .cxx.
> 
> Thanks for noticing. I've just realized that working with Python
> distutils is no that straightforward. It seems that it doesn't have a
> good support for C++ ([1],[2]). There seems to be a patch in [3] that
> I haven't tested it.
> 
> I haven't found a solid resource with information on what is needed to
> build the extension on MacOSX (gcc flags and all that), that Is why I
> preferred Distutils. I would appreciate if anyone could point me to
> some resources that could have that info.

Actually scons has pretty good support for swig (and c++ in general):

SOURCES=['speclib.i'];
env.Append(CPPPATH=['..\\','C:\Python24\include']);
env.Append(CXXFLAGS=['/EHsc']);
(Continue reading)

Ben Webb | 1 Jun 21:24 2006

Re: Help wrapping C++ class [n00b]

Pepe Barbe wrote:
> I haven't found a solid resource with information on what is needed to
> build the extension on MacOSX (gcc flags and all that), that Is why I
> preferred Distutils. I would appreciate if anyone could point me to
> some resources that could have that info.

There are several solutions presented in the SWIG wiki: 
http://www.dabeaz.com/cgi-bin/wiki.pl?SwigFaqMaxOSXSharedLibraries

The one that works for me with Python on OS X 10.4 is:
swig -python -o myext_wrap.c myext.i
gcc -Wall -I/System/Library/Frameworks/Python.framework/Headers -c 
myext_wrap.c
gcc -bundle -flat_namespace -undefined suppress myext_wrap.o -o _myext.so

	Ben
--

-- 
ben <at> salilab.org                      http://salilab.org/~ben/
"It is a capital mistake to theorize before one has data."
	- Sir Arthur Conan Doyle

-------------------------------------------------------
All the advantages of Linux Managed Hosting--Without the Cost and Risk!
Fully trained technicians. The highest number of Red Hat certifications in
the hosting industry. Fanatical Support. Click to learn more
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=107521&bid=248729&dat=121642
Nitro | 1 Jun 21:44 2006

Re: Help wrapping C++ class [n00b]


> Actually scons has pretty good support for swig (and c++ in general):

Yeah, I second that. I use scons for my project and it works really nicely  
together with swig. The only problem is that you need to have it installed  
(so if you want to distribute your app so that it can (has to) be rebuilt  
on another machine, then scons has to be installed there or you have to  
ship it)

-------------------------------------------------------
All the advantages of Linux Managed Hosting--Without the Cost and Risk!
Fully trained technicians. The highest number of Red Hat certifications in
the hosting industry. Fanatical Support. Click to learn more
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=107521&bid=248729&dat=121642
Ratko Jagodic | 1 Jun 22:32 2006
Picon

enum inside std::map

Hi,

I am having a problem with wrapping something like this for use in python:

enum Numbers{ ONE=1 };
std::map<Numbers, std::string> getNumbers();
Numbers getOne();

The problem is that I get a segmentation fault whenever I try to access the key in that hash (the enum) from python. On the other hand, the getOne function that returns "Numbers" works just fine. I must be doing something wrong since I have just started using swig and am still finding my way around it.
I use swig 1.3.29  (just the downloaded version, not the latest cvs).
The files in greater detail are below.
Thanks in advance.

Ratko



/**********    HEADER FILE   ************/
#include <map>
#include <string>

class MyMap {
 public:
  MyMap();
  ~MyMap();
  std::map<Numbers, std::string> getNumbers();
};

enum Numbers{
  ONE = 1,
  TWO = 2
};



/***********   SWIG INTERFACE  ***********/
%module map
%{
#include "map.h"
%}

%include "std_string.i"
%include "std_map.i"

enum Numbers{
  ONE = 1,
  TWO = 2
};

class MyMap {
 public:
  MyMap();
  ~MyMap();
  std::map<Numbers, std::string> getNumbers();
};

%template() std::pair<Numbers, std::string>;
%template(numberHash) std::map<Numbers, std::string>;



/***********   PYTHON TEST FILE  ***********/
import map
m = map.MyMap()
nums = m.getNumbers()
for enum, value in nums.iteritems():
    print "enum = ", enum     #causes SEG FAULT




Sohail Somani | 1 Jun 23:19 2006

RE: enum inside std::map

> -----Original Message-----
> /**********    HEADER FILE   ************/
> #include <map>
> #include <string>
> 
> class MyMap {
>  public:
>   MyMap();
>   ~MyMap();
>   std::map<Numbers, std::string> getNumbers();
> };
> 
> enum Numbers{
>   ONE = 1,
>   TWO = 2
> };

Wouldn't the declaration of Numbers need to come before the declaration
of MyMap?

-------------------------------------------------------
All the advantages of Linux Managed Hosting--Without the Cost and Risk!
Fully trained technicians. The highest number of Red Hat certifications in
the hosting industry. Fanatical Support. Click to learn more
http://sel.as-us.falkag.net/sel?cmd=lnk&kid7521&bid$8729&dat1642

Gmane