472,789 Members | 862 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,789 software developers and data experts.

Using SWIG to build C++ extension

mk
Hello,

I'm having terrible problems building C++ extension to Python 2.4 using
SWIG. I'd appreciate if somebody knowledgeable at the subject took a
look at it. swig-1.3.29, g++ (GCC) 4.1.1 20070105 (Red Hat 4.1.1-52).

I used following commands to build C++ extension:

# swig -c++ -python edit_distance.i
# c++ -c edit_distance.c edit_distance_wrap.cxx edit_distance.cpp -I.
-I/usr/include/python2.4
Linux RH (9.156.44.105) root ~/tmp # c++ -c edit_distance.c
edit_distance_wrap.cxx edit_distance.cpp -I. -I/usr/include/python2.4
c++: edit_distance.cpp: No such file or directory
edit_distance_wrap.cxx: In function ‘PyObject*
_wrap_edit_distance(PyObject*, PyObject*)’:
edit_distance_wrap.cxx:2579: error: ‘string’ was not declared in this scope
edit_distance_wrap.cxx:2579: error: ‘arg1’ was not declared in this scope
edit_distance_wrap.cxx:2580: error: ‘arg2’ was not declared in this scope
edit_distance_wrap.cxx:2597: error: expected type-specifier before ‘string’
edit_distance_wrap.cxx:2597: error: expected `>' before ‘string’
edit_distance_wrap.cxx:2597: error: expected `(' before ‘string’
edit_distance_wrap.cxx:2597: error: expected primary-expression before
‘>’ token
edit_distance_wrap.cxx:2597: error: expected `)' before ‘;’ token
edit_distance_wrap.cxx:2605: error: expected type-specifier before ‘string’
edit_distance_wrap.cxx:2605: error: expected `>' before ‘string’
edit_distance_wrap.cxx:2605: error: expected `(' before ‘string’
edit_distance_wrap.cxx:2605: error: expected primary-expression before
‘>’ token
edit_distance_wrap.cxx:2605: error: expected `)' before ‘;’ token

What's weird is that I _did_ use std:: namespace prefix carefully in the
code:

#include <string>
#include <vector>
#include <iostream>

const unsigned int cost_del = 1;
const unsigned int cost_ins = 1;
const unsigned int cost_sub = 1;
unsigned int edit_distance( std::string& s1, std::string& s2 )
{
const size_t len1 = s1.length(), len2 = s2.length();
std::vector<std::vector<unsigned int d(len1 + 1,
std::vector<unsigned int>(len2 + 1));

for(int i = 1; i <= len1; ++i)
for(int j = 1; j <= len2; ++j)
d[i][j] = std::min(d[i - 1][j] + 1,
std::min(d[i][j - 1] + 1, d[i - 1][j - 1] + (s1[i - 1] == s2[j - 1] ? 0
: 1)));

return d[len1][len2];
}

Ok, anyway I fixed it in the generated code (edit_distance_wrap.cxx). It
compiled to .o file fine then. It linked to _edit_distance.so as well:

# c++ -shared edit_distance_wrap.o -o _edit_distance.so

But now I get import error in Python!

Linux RH root ~/tmp # python
Python 2.4.3 (#1, Dec 11 2006, 11:38:52)
[GCC 4.1.1 20061130 (Red Hat 4.1.1-43)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>import edit_distance
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "edit_distance.py", line 5, in ?
import _edit_distance
ImportError: ./_edit_distance.so: undefined symbol: _Z13edit_distanceRSsS_

What did I do to deserve this? :-)
edit_distance.i file just in case:

%module edit_distance
%{
#include "edit_distance.h"
%}

extern unsigned int edit_distance(string& s1, string& s2);

Jul 11 '08 #1
2 3549
mk wrote:
Hello,

I'm having terrible problems building C++ extension to Python 2.4 using
SWIG. I'd appreciate if somebody knowledgeable at the subject took a
look at it. swig-1.3.29, g++ (GCC) 4.1.1 20070105 (Red Hat 4.1.1-52).

I used following commands to build C++ extension:

# swig -c++ -python edit_distance.i
# c++ -c edit_distance.c edit_distance_wrap.cxx edit_distance.cpp -I.
-I/usr/include/python2.4
Linux RH (9.156.44.105) root ~/tmp # c++ -c edit_distance.c
edit_distance_wrap.cxx edit_distance.cpp -I. -I/usr/include/python2.4
c++: edit_distance.cpp: No such file or directory
edit_distance_wrap.cxx: In function ‘PyObject*
_wrap_edit_distance(PyObject*, PyObject*)’:
edit_distance_wrap.cxx:2579: error: ‘string’ was not declared in this
scope edit_distance_wrap.cxx:2579: error: ‘arg1’ was not declared in this
scope edit_distance_wrap.cxx:2580: error: ‘arg2’ was not declared in this
scope edit_distance_wrap.cxx:2597: error: expected type-specifier before
‘string’ edit_distance_wrap.cxx:2597: error: expected `>' before ‘string’
edit_distance_wrap.cxx:2597: error: expected `(' before ‘string’
edit_distance_wrap.cxx:2597: error: expected primary-expression before
‘>’ token
edit_distance_wrap.cxx:2597: error: expected `)' before ‘;’ token
edit_distance_wrap.cxx:2605: error: expected type-specifier before
‘string’ edit_distance_wrap.cxx:2605: error: expected `>' before ‘string’
edit_distance_wrap.cxx:2605: error: expected `(' before ‘string’
edit_distance_wrap.cxx:2605: error: expected primary-expression before
‘>’ token
edit_distance_wrap.cxx:2605: error: expected `)' before ‘;’ token

What's weird is that I _did_ use std:: namespace prefix carefully in the
code:

#include <string>
#include <vector>
#include <iostream>

const unsigned int cost_del = 1;
const unsigned int cost_ins = 1;
const unsigned int cost_sub = 1;
unsigned int edit_distance( std::string& s1, std::string& s2 )
{
const size_t len1 = s1.length(), len2 = s2.length();
std::vector<std::vector<unsigned int d(len1 + 1,
std::vector<unsigned int>(len2 + 1));

for(int i = 1; i <= len1; ++i)
for(int j = 1; j <= len2; ++j)
d[i][j] = std::min(d[i - 1][j] + 1,
std::min(d[i][j - 1] + 1, d[i - 1][j - 1] + (s1[i - 1] == s2[j - 1] ? 0
: 1)));

return d[len1][len2];
}

Ok, anyway I fixed it in the generated code (edit_distance_wrap.cxx). It
compiled to .o file fine then. It linked to _edit_distance.so as well:

# c++ -shared edit_distance_wrap.o -o _edit_distance.so

But now I get import error in Python!

Linux RH root ~/tmp # python
Python 2.4.3 (#1, Dec 11 2006, 11:38:52)
[GCC 4.1.1 20061130 (Red Hat 4.1.1-43)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>import edit_distance
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "edit_distance.py", line 5, in ?
import _edit_distance
ImportError: ./_edit_distance.so: undefined symbol: _Z13edit_distanceRSsS_

What did I do to deserve this? :-)
edit_distance.i file just in case:

%module edit_distance
%{
#include "edit_distance.h"
%}

extern unsigned int edit_distance(string& s1, string& s2);
Hello,

I took your example files and did the following:
changed the #include "edit_distance.h" to #include "edit_distance.c"
in the edit_distance.i file.
Then I changed the first few lines of your function definition
unsigned int edit_distance( const char* c1, const char* c2 )
{
std::string s1( c1), s2( c2);
and also adapted the signature in the edit_distance.i file.
Then
swig -shadow -c++ -python edit_distance.i
g++ -c -fpic -I/usr/include/python edit_distance_wrap.cxx
g++ -shared edit_distance_wrap.o -o _edit_distance.so

I could import edit_distance without any error messages
>>import edit_distance
print edit_distance.edit_distance( "toto", "titi")
2
>>print edit_distance.edit_distance( "toto", "toti")
1

Perhaps I changed too many things, but this may get you started,

Regards,

Bas
Jul 11 '08 #2
mk
Hello Bas,

Thanks, man! Your recipe worked on Debian system, though not on RedHat,
and I still have no idea why. :-) Anyway, I have it working. Thanks again.

I took your example files and did the following:
changed the #include "edit_distance.h" to #include "edit_distance.c"
in the edit_distance.i file.
Then I changed the first few lines of your function definition
unsigned int edit_distance( const char* c1, const char* c2 )
{
std::string s1( c1), s2( c2);
and also adapted the signature in the edit_distance.i file.
Then
swig -shadow -c++ -python edit_distance.i
g++ -c -fpic -I/usr/include/python edit_distance_wrap.cxx
g++ -shared edit_distance_wrap.o -o _edit_distance.so
Jul 13 '08 #3

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

Similar topics

0
by: Phil Schmidt | last post by:
I'm attempting to follow the instructions at http://sebsauvage.net/python/mingw.html, without luck. I have Python 2.3, MinGW 3.2.3, ans SWIG 1.3.19. I get an export error on "initexample" when...
7
by: Carl Waldbieser | last post by:
I tried to adapt the instructions for building the M2Crypto module (http://sandbox.rulemaker.net/ngps/m2/INSTALL.html) to build a version compatible with Python2.3, but I've had some mixed results....
0
by: Helmut Zeisel | last post by:
I want to build a static extension of Python using SWIG and VC++ 6.0 as described in http://www.swig.org/Doc1.3/Python.html#n8 for gcc. My file is testerl.i: ========================= %module...
9
by: timothy.williams | last post by:
Hi. I trying to write an extension module to call some C libraries so I can use them in Python. Several of the library functions pass pointers to structures as arguments. I was thinking that I...
4
by: Alexander Eisenhuth | last post by:
Hi alltogether, I use ActivePython 2.4.1 , also the debug part from http://ftp.activestate.com/ActivePython/etc/ and VC 6.0 unter Windows XP. I can't figure out howto debug my c++ extension....
2
by: mr_gees100_peas | last post by:
Hi, I've been trying for days to make either boost.python or swig to work for me. The one I have gotten the closest to is boost. Note that this is for windows XP. I'm not much of an unix person...
2
by: piyushtiwari | last post by:
I have to call a C++ function in php. I tried it using swig. I wrote the interface file example.i to call the functions from example.cxx in php. To build a PHP extension, I run swig using the -php...
0
by: mauro | last post by:
Hi all, I am trying to make a package distribution containing some extension module written in C. The problem is that when I run setup.py some files are generated in the wrong position. Suppose...
0
by: Eric von Horst | last post by:
Hi, we have a third-party product that has a C++ api on HP-UX. I would like be able to use the API in Python (as I remember Python is good at doing this). I have no experience with this so...
0
by: Rina0 | last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.