SWIG: 'module' object has no attribute 'Decklist'
Asked Answered
O

1

6

I'm having one hell of a time with SWIG, due in part to the lack of good C++ examples to learn from. I finally got my first program to compile with SWIG, but am having troubles running it. Let me just get right to the code...

setup.py:

#!/usr/bin/env python

"""
setup.py file for SWIG example
"""

from distutils.core import setup, Extension


decklist_module = Extension('_decklist',
                           sources=['decklist_wrap.cxx', 'decklist.cpp'],
                           )

setup (name = 'decklist',
       version = '0.1',
       author      = "Me",
       description = """Testing!""",
       ext_modules = [decklist_module],
       py_modules = ["decklist"],
       )

decklist.hpp:

#include <boost/unordered_map.hpp>



class DeckList{
   private:
        boost::unordered_map<std::string, int> mainBoard;
        boost::unordered_map<std::string, int> sideBoard;
    public:
        void addCard(std::string name, int cardCount);
        int getCount(std::string cardName);
        DeckList();
        ~DeckList();

};

decklist.cpp:

#ifndef DECKLIST_H
#define DECKLIST_H
#include "decklist.hpp"
#include <stdio.h>

DeckList::DeckList(){

}

void DeckList::addCard(std::string cardName, int cardCount){
    mainBoard[cardName] = cardCount;
}

int DeckList::getCount(std::string cardName){
    return mainBoard[cardName];
}

#endif    

decklist.i:

//decklist.i
%module decklist
%{
    #include "decklist.hpp"
%}
#include "decklist.hpp"

Now on the terminal (I am on Ubuntu Natty Narwhal), I run the following two commands:

swig -python -c++ decklist.i
python setup.py build_ext --inplace

The second gives me the following response:

running build_ext
building '_decklist' extension
gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fPIC -I/usr/include/python2.7 -c decklist_wrap.cxx -o build/temp.linux-x86_64-2.7/decklist_wrap.o
cc1plus: warning: command line option "-Wstrict-prototypes" is valid for Ada/C/ObjC but not for C++
gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fPIC -I/usr/include/python2.7 -c decklist.cpp -o build/temp.linux-x86_64-2.7/decklist.o
cc1plus: warning: command line option "-Wstrict-prototypes" is valid for Ada/C/ObjC but not for C++
g++ -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions -Wl,-Bsymbolic-functions build/temp.linux-x86_64-2.7/decklist_wrap.o build/temp.linux-x86_64-2.7/decklist.o -o /home/aespiel1/deck/_decklist.so

But I wind up with:

decklist.cpp
decklist.hpp
decklist.i
decklist.py
decklist.pyc
_decklist.so
decklist_wrap.cxx
setup.py

and a build folder with .o files for both the decklist_wrap and decklist files.

If I run python in idle and switch into this directory and:

import decklist

I get:

Traceback (most recent call last):
  File "<pyshell#2>", line 1, in <module>
      import decklist
ImportError: No module named decklist

Strangely, if I run it from the terminal, I can import decklist. But then a command like:

dl = decklist.DeckList()  

gives:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'DeckList'

What am I doing wrong? I am so frustrated.

Oller answered 5/8, 2011 at 17:45 Comment(1)
I have a small Python/C++/Swig example to check against.Freewheeling
L
2

change decklist.i as following:

//decklist.i
%module decklist
%{
    #include "decklist.hpp"
%}
%include "decklist.hpp" // <-- *** use % in *.i  ***

or you can declare your classes & functions here that you want to export.

Longshore answered 5/8, 2011 at 22:15 Comment(3)
Okay. I won't be able to try this until Monday...I will report back then. Thanks for the advice.Oller
Wait, that may have been my mistake now. I realized I made another mistake when trying it this morning. I will try again shortly.Oller
No, I am now getting the error: import decklist Traceback (most recent call last): File "<stdin>", line 1, in <module> File "decklist.py", line 25, in <module> _decklist = swig_import_helper() File "decklist.py", line 21, in swig_import_helper _mod = imp.load_module('_decklist', fp, pathname, description) ImportError: ./_decklist.so: undefined symbol: _ZN8DeckListD1Ev when running on python from the terminal. Does anyone know what that means?Oller

© 2022 - 2024 — McMap. All rights reserved.