c++ - SWIG: 'module' 对象没有属性 'Decklist'

标签 c++ python gcc swig python-idle

我在 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"],
       )

牌组列表.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();

};

牌组.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    

牌组.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.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

我得到:

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'

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

最佳答案

改变牌组列表.i如下:

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

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

关于c++ - SWIG: 'module' 对象没有属性 'Decklist',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6960470/

相关文章:

c++ - 在 libc++ 的 istringstream 的析构函数中未定义对运算符 delete 的引用

c++ - 防止友元函数访问类的私有(private)成员

c++ - getline c++ 没有匹配函数

c# - 将 XSD 转换为 SQL 关系表

c++ - gcc 中是否有 stdext::hash_map 的等价物

c++ - C++ (GCC) 中的四倍精度

c++ - 由于段错误导致生产机器上的 C++ 应用程序崩溃

python - 从 OpenCV + Python 获取 HOG 图像特征?

python - 如何使用 3d numpy 数组的第一个波段作为所有其他波段的虚值

python - Django模型表单: Exclude a field which is not in Model