473,779 Members | 1,884 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Wrapping C++ class with SWIG, Mac OS X

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=Wavele t
swig -c++ -python -o ${MOD_NAME}_wra p.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}_wra p.cpp -o ${MOD_NAME}_wra p.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}_wra p.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< doubletheV);
~Wavelet();
vector<doubleGe tDaub4Trans();

private:
vector<doublev;

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

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

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

vector<doubleWa velet::GetDaub4 Trans()
{
vector<doublere tV = 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_Cp p/_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.
Jun 27 '08 #1
3 5610
Paul Anton Letnes schrieb:
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=Wavele t
swig -c++ -python -o ${MOD_NAME}_wra p.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}_wra p.cpp -o ${MOD_NAME}_wra p.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}_wra p.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< doubletheV);
~Wavelet();
vector<doubleGe tDaub4Trans();

private:
vector<doublev;

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

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

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

vector<doubleWa velet::GetDaub4 Trans()
{
vector<doublere tV = 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_Cp p/_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!
Can't help on SWIG - all I can say is that SIP which is used to wrap the
large and template-ridden C++-GUI-Toolkit Qt worked flawlessly for me.
Maybe you should try that.

Diez
Jun 27 '08 #2
Okay, installed SIP. Looks promising, following the tutorial on
http://www.riverbankcomputing.com/st...html#using-sip
It should be noted that I am working on a Mac - I know there are some
differences, but it's still UNIX and should work somehow.

Anyway, I copy-paste and create the Word.h header, write an
implementation in Word.cpp, the SIP wrapper Word.sip and the
configure.py script. I now run configure and make, creating the
following error:

~/Desktop/SIP_example $ python configure.py
~/Desktop/SIP_example $ make
c++ -c -pipe -fPIC -Os -Wall -W -I. -I/sw/include/python2.5 -o
sipwordcmodule. o sipwordcmodule. cpp
c++ -c -pipe -fPIC -Os -Wall -W -I. -I/sw/include/python2.5 -o
sipwordWord.o sipwordWord.cpp
c++ -headerpad_max_i nstall_names -bundle -undefined dynamic_lookup -o
word.so sipwordcmodule. o sipwordWord.o -lword
ld: library not found for -lword
collect2: ld returned 1 exit status
make: *** [word.so] Error 1
~/Desktop/SIP_example $

SWIG at least works nicely with C... Too bad I know so little about
compilers and libraries, I don't quite understand what the linker (ld)
is complaining about. The simplest tutorial should anyway work?
Cheers
Paul.

>
Can't help on SWIG - all I can say is that SIP which is used to wrap
the
large and template-ridden C++-GUI-Toolkit Qt worked flawlessly for me.
Maybe you should try that.

Diez
--
http://mail.python.org/mailman/listinfo/python-list
Jun 27 '08 #3
Paul Anton Letnes schrieb:
Okay, installed SIP. Looks promising, following the tutorial on
http://www.riverbankcomputing.com/st...html#using-sip
It should be noted that I am working on a Mac - I know there are some
differences, but it's still UNIX and should work somehow.

Anyway, I copy-paste and create the Word.h header, write an
implementation in Word.cpp, the SIP wrapper Word.sip and the
configure.py script. I now run configure and make, creating the
following error:

~/Desktop/SIP_example $ python configure.py
~/Desktop/SIP_example $ make
c++ -c -pipe -fPIC -Os -Wall -W -I. -I/sw/include/python2.5 -o
sipwordcmodule. o sipwordcmodule. cpp
c++ -c -pipe -fPIC -Os -Wall -W -I. -I/sw/include/python2.5 -o
sipwordWord.o sipwordWord.cpp
c++ -headerpad_max_i nstall_names -bundle -undefined dynamic_lookup -o
word.so sipwordcmodule. o sipwordWord.o -lword
ld: library not found for -lword
collect2: ld returned 1 exit status
make: *** [word.so] Error 1
~/Desktop/SIP_example $

SWIG at least works nicely with C... Too bad I know so little about
compilers and libraries, I don't quite understand what the linker (ld)
is complaining about. The simplest tutorial should anyway work?
Not knowing C/C++ & linking is certainly something that will get you
when trying to wrap libs written in these languages.

I'm on OSX myself, and can say that as a unixish system, it is rather
friendly to self-compliation needs.

However, without having the complete sources & libs, I can't really
comment much - the only thing that is clear from above is that the
linker does not find the file

libword.dylib

which you of course need to have somewhere. The good news is that once
you've teached the linker where to find it (using LDFLAGS or other
means) you are (modulo debugging) done - SIP has apparently grokked your
..sip-file.

Of course you also must make the library available at runtime. So it
must be installed on a location (or that location given with
DYLD_LIBRARY_PA TH) where the dynamic loader will find it.

Diez
Jun 27 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
3861
by: Roy Smith | last post by:
I've got a C library with about 50 calls in it that I want to wrap in Python. I know I could use some tool like SWIG, but that will give me a too-literal translation; I want to make some modifications along the way to make the interface more Pythonic. For example, all of these functions return an error code (typically just errno passed along, but not always). They all accept as one of their arguments a pointer to someplace to store...
6
1863
by: Andrew Wilkinson | last post by:
Hi, I'm trying to wrap a C++ class hierarchy with Python types and I'd like to maintain the hierarchy in the types. I'm fairly sure this is possible, isn't it? Are there any documents explaining how to do this, the standard Python manual doesn't go into enough detail about creating types for this. I've found the tp_base and tp_bases elements and I've set them to the base
2
2067
by: Glenn Pierce | last post by:
Hi I have been trying to wrap a c library called FreeImage for python and am having trouble with a couple of functions. One function is FreeImage_Load which takes parameters (enum, const char*, int) I have wrapped the function with the following code static PyObject * freeimage_load(PyObject *self, PyObject *args)
0
1241
by: James Carroll | last post by:
Hi, I asked this on the SWIG mailing list, but it's pretty dead over there.... I'm trying to get Python to pass a subclass of a C++ object to another C++ object... I have three C++ classes, TiledImageSource ZoomifyReaderWx which ISA TiledImageSource
0
214
by: Paul Anton Letnes | last post by:
>
1
5344
by: Stodge | last post by:
Yet another SWIG question (YASQ!). I'm having a problem with using an abstract base class. When generating the Python bindings, SWIG thinks that all the concrete classes that derive from this abstract class are abstract too and won't create the correct constructor. Abstract class: class CORE_API Shape
6
6273
by: Adam C. | last post by:
We have a situation where we want a Swig-generated Python class to have a different base (not object). It doesn't appear that we can coerce Swig into generating the class we want at present (but we are still enquiring). Is it possible to dynamically change the base class to something else? Initial experiments appear to show it is not: -------------------------------- snip -------------------------------- pass
8
2285
by: Anish Chapagain | last post by:
Hi!! I tried wrapping a simple C code suing SWIG to Python, but am having problem, my .c file is, Step 1: example.c -------------- double val=3.0; int fact(int n)
5
4310
by: Charlie | last post by:
Hi All, I am new to using swig/C++/python. I got some problem with function pointers. I posted in swig-user, but got no response. So I forwarded it here. You help is greatly appreciated. Thanks! ---------- Forwarded message ----------
0
9632
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9471
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10136
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10071
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9925
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8958
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6723
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5501
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4036
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.