#include #include #include using namespace std; using namespace boost::python; struct World { void set(std::string msg) { this->msg = msg; } std::string greet() { return msg; } std::string msg; }; BOOST_PYTHON_MODULE(hello) { class_("World") .def("greet", &World::greet) .def("set", &World::set) ; } int main(int argc, char *argv[]) { Py_Initialize(); try { inithello(); } catch (error_already_set) { PyErr_Print(); } PyRun_SimpleString("import hello"); PyRun_SimpleString("planet = hello.World()"); PyRun_SimpleString("planet.set('howdy')"); PyRun_SimpleString("print planet.greet()"); PyObject *pModule = PyImport_ImportModule(entry); if (pModule == NULL) { if (PyErr_Occurred()) PyErr_Print(); else fprintf(stderr, " No Python exception information available\n"); return 1; } // pDict is a borrowed reference PyObject *pDict = PyModule_GetDict(pModule); // pFunc is also a borrowed reference PyObject *pFunc = PyDict_GetItemString(pDict, "run"); if (PyCallable_Check(pFunc)) { PyObject_CallObject(pFunc, NULL); } else { PyErr_Print(); } Py_DECREF(pModule); Py_Finalize(); return 0; }