SWIG:"模块"对象没有属性"Decklist"

SWIG: 'module' object has no attribute 'Decklist'

本文关键字:属性 Decklist 对象 模块 SWIG      更新时间:2023-10-16

我在使用SWIG的过程中经历了一段痛苦的时光,部分原因是缺乏好的c++示例来学习。我终于得到了我的第一个程序与SWIG编译,但我有麻烦运行它。让我直接进入代码…

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"

现在在终端上(我使用的是Ubuntu Natty Narwhal),我运行以下两个命令:

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

第二个命令给出如下响应:

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

但是我最后写了:

<>之前decklist.cppdecklist.hppdecklist.idecklist.pydecklist.pyc_decklist.sodecklist_wrap.cxxsetup . py之前

和一个包含.o文件的构建文件夹用于decklist_wrapdecklist文件。

如果我在空闲状态下运行python并切换到这个目录并:

import decklist

:

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

奇怪的是,如果我从终端运行它,我可以import decklist。然后输入如下命令:

dl = decklist.DeckList()  

给:

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

我做错了什么?

change decklist。

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

或者你可以声明你的类&要导出的函数