SWIG:"模块"对象没有属性"Decklist"
SWIG: 'module' object has no attribute 'Decklist'
我在使用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_wrap
和decklist
文件。
如果我在空闲状态下运行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 ***
或者你可以声明你的类&要导出的函数
相关文章:
- 如何导出包含具有"std::unique_ptr"值的"std::map"属性的
- C++概念:如何使用'concept'检查模板化结构的属性?
- 子目录是否继承属性,例如add_definitions,include_directories和父Cmakelist.t
- 通过指向指针数组的指针访问子类的属性
- MSVC是否支持C++11样式的属性而不是__declspec
- QML:修改在不同QML文件(而非main.QML)中定义的子对象的属性
- 类的C++属性似乎已重新初始化
- Qt Quick-如何仅从c++代码与qml属性交互
- 用概念检查属性的类型
- C++如何将同一类的实例作为属性
- 按多个属性排序
- 主函数参数的属性
- "perf_event_attr"结构的"read_format"属性的选项到底是什么?
- C++删除未使用的类属性会导致 std::logic_error
- 无法使用 SWIG 在 Python 中实例化C++类(获取属性错误)
- 使用内存地址访问结构的属性值
- C++调用具有 *this 属性的单个帮助程序函数
- C++ 在堆栈中包含多态属性的类对象存储
- C++ 命名参数习惯用语 - 未设置字符串属性
- SWIG:"模块"对象没有属性"Decklist"