Wrapping C++ class with SWIG, Mac OS X

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Paul Anton Letnes

    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.
  • Diez B. Roggisch

    #2
    Re: Wrapping C++ class with SWIG, Mac OS X

    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

    Comment

    • Paul Anton Letnes

      #3
      Re: Wrapping C++ class with SWIG, Mac OS X

      Okay, installed SIP. Looks promising, following the tutorial on

      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

      Comment

      • Diez B. Roggisch

        #4
        Re: Wrapping C++ class with SWIG, Mac OS X

        Paul Anton Letnes schrieb:
        Okay, installed SIP. Looks promising, following the tutorial on

        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

        Comment

        Working...