SWIG:“模块”对象没有属性“Decklist”;

发布于 2024-11-27 22:19:26 字数 3290 浏览 1 评论 0原文

我在 SWIG 上度过了一段痛苦的时光,部分原因是缺乏可供学习的优秀 C++ 示例。我终于得到了第一个用 SWIG 编译的程序,但运行它时遇到了麻烦。让我直接开始编写代码...

setup.py:decklist.hpp:decklist.cpp:decklist.i

#!/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"],
       )

我在Ubuntu

#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();

};

现在在终端上(

#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
%module decklist
%{
    #include "decklist.hpp"
%}
#include "decklist.hpp"

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

和一个包含 .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

奇怪的是,如果我从终端运行它,我可以导入牌组列表。但是像这样的命令

dl = decklist.DeckList()  

会给出:

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

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

I'm having one hell of a time with SWIG, due in part to the lack of good C++ examples to learn from. I finally got my first program to compile with SWIG, but am having troubles running it. Let me just get right to the code...

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"

Now on the terminal (I am on Ubuntu Natty Narwhal), I run the following two commands:

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

The second gives me the following response:

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

But I wind up with:

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

I get:

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

Strangely, if I run it from the terminal, I can import decklist. But then a command like:

dl = decklist.DeckList()  

gives:

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

What am I doing wrong? I am so frustrated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

神仙妹妹 2024-12-04 22:19:26

更改 Decklist.i 如下:

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

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

change decklist.i as following:

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

or you can declare your classes & functions here that you want to export.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文