我试图使用swig将c++函数包装到python中。我使用以下命令swig -c++ -python helloworld.i
g++ -O2 -fPIC -c hello.cpp
g++ -O2 -fPIC -c helloworld_wrap.cxx -I//anaconda/include/python2.7
g++ -lpython -dynamclib hello.o helloworld_wrap.o -o _helloworld.so
其中hello.cpp是带函数的初始文件,helloworld.i是带包装的文件。这些命令创建库helloworld,但我只能通过/usr/bin/python中的默认python导入它
如果我试图通过通过通过anaconda安装的python导入它,则会出现以下错误:
^{pr2}$
你能告诉我如何用Python的python来包装代码吗?在
在上面的问题中,最上面的答案给出了使用disutils和python中的一个设置文件来构建库的解释。这真是奇迹:)
包装简单类的下一个问题是:/* File : example.h */
class Shape {
public:
Shape() {
nshapes++;
}
virtual ~Shape() {
nshapes--;
};
double x, y;
void move(double dx, double dy);
virtual double area() = 0;
virtual double perimeter() = 0;
static int nshapes;
};
class Circle : public Shape {
private:
double radius;
public:
Circle(double r) : radius(r) { };
virtual double area();
virtual double perimeter();
};
class Square : public Shape {
private:
double width;
public:
Square(double w) : width(w) { };
virtual double area();
virtual double perimeter();
};
我的setup.py文件:#setup.py file:
from setuptools import setup, Extension
setup(name='example',
version='0.1',
ext_modules=[Extension('_example', ['example.h', 'example.i'],
swig_opts=['-c++'],
)],
)
我用来包装的代码:python setup.py build_ext --inplace
错误消息:running build_ext
building '_example' extension
swigging example.i to example_wrap.cpp
swig -python -c++ -o example_wrap.cpp example.i
error: unknown file type '.h' (from 'example.h')
你能告诉我这里怎么了吗。我想它不会识别“.h”文件,但由于它是头文件,所以我认为它可以保持原样。另外,我的setup.py文件是否正确?我只是想以简单包装为例,网上显然没有简单的教程。
我也可以在其他不同的问题上问这个问题,但我想现在就继续。在