gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:44:28 24 4
gpt4 key购买 nike

我在 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.cppdecklist.hppdecklist.idecklist.pydecklist.pyc_decklist.sodecklist_wrap.cxxsetup.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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com