Python Forum

Full Version: Calling Extended Embedding Python as shared library
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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
#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;

}
NEW MAIN FILE
int main(int argc, char *argv[]){
    runSomething();
    return 0;
}
How do I make this work?