Jul-19-2019, 05:22 PM
I am running Python3.6
I am trying to run the example:
https://docs.python.org/3.6/extending/em...ded-python
I would like do to the same thing using shared library. I changed the main(..) to runSomething(..).
I create a shared library from the file.
I created another file with main(..) calling runSomething(..).
SHARED LIBRARY
NEW MAIN FILE
How do I make this work?
I am trying to run the example:
https://docs.python.org/3.6/extending/em...ded-python
I would like do to the same thing using shared library. I changed the main(..) to runSomething(..).
I create a shared library from the file.
I created another file with main(..) calling runSomething(..).
SHARED LIBRARY
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
#include </usr/include/python3.6m/Python.h> static int numargs = 0 ; / * Return the number of arguments of the application command line * / static PyObject * emb_numargs(PyObject * self , PyObject * args) { if (!PyArg_ParseTuple(args, ":numargs" )) return NULL; return PyLong_FromLong(numargs); } static PyMethodDef EmbMethods[] = { { "numargs" , emb_numargs, METH_VARARGS, "Return the number of arguments received by the process." }, {NULL, NULL, 0 , NULL} }; static PyModuleDef EmbModule = { PyModuleDef_HEAD_INIT, "emb" , NULL, - 1 , EmbMethods, NULL, NULL, NULL, NULL }; static PyObject * PyInit_emb(void) { return PyModule_Create(&EmbModule); } int runSomething( int argc, char * argv[]){ numargs = argc; PyImport_AppendInittab( "emb" , &PyInit_emb); Py_Initialize(); PyRun_SimpleString( "import emb" ); PyRun_SimpleString( "print('Number of arguments', emb.numargs())" ); Py_Finalize(); return 0 ; } |
1 2 3 4 |
int main( int argc, char * argv[]){ runSomething(); return 0 ; } |