Reputation: 259
How can I extend an embedded interpreter with C++ code? I have embedded the interpreter and I can use boost.python to make a loadable module (as in a shared library) but I don't want the library floating around because I want to directly interface with my C++ application. Sorry if my writing is a bit incoherent.
Upvotes: 0
Views: 844
Reputation: 42627
At least for the 2.x interpreters: you write your methods as C-style code with PyObject* return values. They all basically look like:
PyObject* foo(PyObject *self, PyObject *args);
Then, you collect these methods in a static array of PyMethodDef:
static PyMethodDef MyMethods[] =
{
{"mymethod", foo, METH_VARARGS, "What my method does"},
{NULL, NULL, 0, NULL}
};
Then, after you've created and initialized the interpreter, you can add these methods "into" the interpreter via the following:
Py_InitModule("modulename", MyMethods);
You can refer now to your methods via the modulename you've declared here.
Some additional info here: http://www.eecs.tufts.edu/~awinsl02/py_c/
Upvotes: 2