37,742
社区成员
发帖
与我相关
我的任务
分享
using namespace boost::python;
void hello()
{
std::cout<<"Hello from C :)"<<std::endl;
}
BOOST_PYTHON_MODULE(helloModule)
{
def("hello", hello);
}
# define BOOST_PYTHON_MODULE BOOST_PYTHON_MODULE_INIT
# define BOOST_PYTHON_MODULE_INIT(name) \
void BOOST_PP_CAT(init_module_,name)(); \
extern "C" __declspec(dllexport) _BOOST_PYTHON_MODULE_INIT(name)
# define _BOOST_PYTHON_MODULE_INIT(name) \
PyObject* BOOST_PP_CAT(PyInit_, name)() \
{ \
static PyModuleDef_Base initial_m_base = { \
PyObject_HEAD_INIT(NULL) \
0, /* m_init */ \
0, /* m_index */ \
0 /* m_copy */ }; \
static PyMethodDef initial_methods[] = { { 0, 0, 0, 0 } }; \
\
static struct PyModuleDef moduledef = { \
initial_m_base, \
BOOST_PP_STRINGIZE(name), \
0, /* m_doc */ \
-1, /* m_size */ \
initial_methods, \
0, /* m_reload */ \
0, /* m_traverse */ \
0, /* m_clear */ \
0, /* m_free */ \
}; \
\
return boost::python::detail::init_module( \
moduledef, BOOST_PP_CAT(init_module_, name) ); \
} \
void BOOST_PP_CAT(init_module_, name)()
Now, inside main() and before the call to Py_Initialize() we want to call PyImport_AppendInittab( "CppMod", &initCppMod ); initCppMod is a function created by the BOOST_PYTHON_MODULE macro which is used to initialize the Python module CppMod. At this point, your embedded python script may call import CppMod and then access CppClass as a member of the module.