【问题标题】:SWIG: 'module' object has no attribute 'Decklist'SWIG:“模块”对象没有属性“甲板清单”
【发布时间】:2011-08-05 17:45:56
【问题描述】:

我在 SWIG 上玩得很开心,部分原因是缺乏好的 C++ 示例可供学习。我终于用 SWIG 编译了我的第一个程序,但是在运行它时遇到了麻烦。让我直接进入代码...

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"

现在在终端上(我在 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

但我最终得到:

甲板清单.cpp 甲板清单.hpp 甲板清单.i 甲板清单.py 甲板清单.pyc _decklist.so 甲板清单包装.cxx 安装程序.py

以及包含decklist_wrapdecklist 文件的.o 文件的构建文件夹。

如果我在空闲状态下运行 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'

我做错了什么?我很沮丧。

【问题讨论】:

标签: c++ python gcc swig python-idle


【解决方案1】:

将decklist.i更改如下:

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

或者您可以在此处声明要导出的类和函数。

【讨论】:

  • 好的。直到星期一我才能尝试这个......我会在那时报告。感谢您的建议。
  • 等等,这可能是我现在的错误。我意识到我今天早上尝试时又犯了一个错误。我很快会再试一次。
  • 不,我现在收到错误消息:import decklist Traceback (most recent call last): File "", line 1, in File "decklist.py", line 25 ,在 _decklist = swig_import_helper() 文件“decklist.py”,第 21 行,在 swig_import_helper _mod = imp.load_module('_decklist', fp, pathname, description) ImportError: ./_decklist.so: undefined symbol: _ZN8DeckListD1Ev从终端在 python 上运行时。有谁知道这是什么意思?
猜你喜欢
  • 2021-09-25
  • 2017-03-29
  • 2013-02-13
  • 2010-11-18
  • 2019-01-03
  • 2017-05-19
  • 2013-02-01
  • 2018-02-27
相关资源
最近更新 更多