2011-08-05 48 views
7

我與SWIG有一段時間,部分原因是缺乏良好的C++示例來學習。我終於得到了我的第一個程序來與SWIG編譯,但我有麻煩運行它。讓我得到正確的代碼...SWIG:'模塊'對象沒有屬性'Decklist'

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  

套牌。 i:

//decklist.i 
%module decklist 
%{ 
    #include "decklist.hpp" 
%} 
#include "decklist.hpp" 

現在終端(我是在Ubuntu納蒂獨角鯨),我運行以下兩個命令:

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 FIL一個build文件夾ES。

如果我運行在空閒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' 

我在做什麼錯?我很沮喪。

+0

我有一個[小的Python/C++ /痛飲示例](https://github.com/martinxyz/python/tree/master/realistic)要檢查的。 – maxy

回答

2

變化decklist.i如下:

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

,或者你可以在這裏&功能,你要導出聲明你的類。

+0

好的。我將無法在星期一之前嘗試此操作......我會在那時報告。感謝您的建議。 – user650261

+0

沒有好,我仍然得到同樣的錯誤:-( – user650261

+0

等待,這可能是我錯了。我意識到今天早上想它時,我又錯了。我會稍後重試。 – user650261

相關問題