좋은 C++ 예제가 부족하여 SWIG와 시간을 많이 보냅니다. 드디어 SWIG로 컴파일하는 첫 번째 프로그램을 얻었지만 문제가 발생했습니다.SWIG : 'module'객체에 '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
decklist. i :
//decklist.i
%module decklist
%{
#include "decklist.hpp"
%}
#include "decklist.hpp"
터미널 (I 우분투 단정 한 일각 고래 오전)에 지금
691,363,210, 나는 다음과 같은 두 가지 명령을 실행합니다
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
과 decklist_wrap
및 decklist
FIL 모두 .o
파일과 빌드 폴더 es. 내가 유휴 상태에서 파이썬을 실행하고이 디렉토리로 전환 한 경우
는 :
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'
? 나는 너무 좌절한다. 다음과 같이
확인할 수있는 [작은 파이썬/C++/Swig 예제] (https://github.com/martinxyz/python/tree/master/realistic)가 있습니다. – maxy