Embedding Python and adding C functions to the interpreter
I'm currently writing an applications that embedds the python interpreter. The idea is to have the program call user specified s开发者_运维知识库cripts on certain events in the program. I managed this part but now I want the scripts to be able to call functions in my program.
Here's my code so far:
#include "python.h"
static PyObject* myTest(PyObject* self,PyObject *args)
{
return Py_BuildValue("s","123456789");
}
static PyMethodDef myMethods[] = {{"myTest",myTest},{NULL,NULL}};
int main()
{
Py_Initialize();
Py_InitModule("PROGRAM",myMethods);
PyRun_SimpleString("print PROGRAM.myTest()");
Py_Finalize();
}
Thanks!
You need to bind that function to some module, see http://docs.python.org/extending/embedding.html#extending-embedded-python
Edit: Basicly your code should work. Whats not working?
精彩评论