新建工程文件夹,包含三个文件helloworld.cpp、helloworld.py与CMakeLists.txt
在helloworld.cpp中编写:
// 因为采用静态编译boost库,因此必须定义此宏,否则编译出错
#define BOOST_PYTHON_STATIC_LIB
#include<boost/python.hpp>
#include<boost/python/wrapper.hpp>
#include<string>
#include<iostream>
using namespace boost::python;
using namespace std;
struct Base
{
virtual ~Base() {}
virtual int f() { return 0; };
};
struct BaseWrap : Base, wrapper<Base>
{
int f()
{
if (override f = this->get_override("f"))
return f(); //如果函数进行重载了,则返回重载
return Base::f(); //否则返回基类
}
int default_f() { return this->Base::f(); }
};
BOOST_PYTHON_MODULE(hello)
{
class_<BaseWrap, boost::noncopyable>("Base")