简体   繁体   中英

Calling C++ functions from Python (Anaconda)

Update:

So I have gotten my C++ code to compile, as per the docs using setup.py as shown in section 4.1 here , and it seems the module is being successfully imported as when I query assignment1.add? after importing I receive the information:

Docstring: Add two numbers.

Type: builtin_function_or_method

However, when I actually call the function assignment1.sum(1,2) the Python kernel immediately dies with no further error message than "Kernel died, restarting".

#include <Python.h>

static PyObject * assignment1_add(PyObject *self, PyObject *args)
{
    int *a, *b;
    int sum;

    if (!PyArg_ParseTuple(args, "ii", &a, &b))
        return NULL;
    sum = *a + *b;
    return PyLong_FromLong(sum);
}

static PyMethodDef Assignment1Methods[] = {
    {"add",  assignment1_add, METH_VARARGS, "Add two numbers."},
    {NULL, NULL, 0, NULL}        /* Sentinel */
};

static struct PyModuleDef assignment1module = {
    PyModuleDef_HEAD_INIT,
    "assignment1",   /* name of module */
    NULL, /* module documentation, may be NULL */
    -1,       /* size of per-interpreter state of the module,
              or -1 if the module keeps state in global variables. */
    Assignment1Methods
};

PyMODINIT_FUNC PyInit_assignment1(void)
{
    PyObject *m;

    m = PyModule_Create(&assignment1module);
    if (m == NULL)
        return NULL;
    return m;
}

int
main(int argc, char *argv[])
{
    wchar_t *program = Py_DecodeLocale(argv[0], NULL);
    if (program == NULL) {
        fprintf(stderr, "Fatal error: cannot decode argv[0]\n");
        exit(1);
    }

    /* Add a built-in module, before Py_Initialize */
    PyImport_AppendInittab("assignment1", PyInit_assignment1);

    /* Pass argv[0] to the Python interpreter */
    Py_SetProgramName(program);

    /* Initialize the Python interpreter.  Required. */
    Py_Initialize();

    /* Optionally import the module; alternatively,
    import can be deferred until the embedded script
    imports it. */
    PyImport_ImportModule("assignment1");

    PyMem_RawFree(program);
    return 0;
}

Any suggestions as to where I should look next for the cause of the problem?

Your first attempt uses Python3 API. I'm not sure what "linker error 1120" is, not going to look it up, but my guess is undefined reference, which is totally understandable if you are trying to use Python2.7 (why by the way?) Python2 and Python3 have different incompatible C APIs.

The second attempt uses Python2.7. The example you have copied is wrong. The init function to initialise a module named module should be called initmodule , not initmod . But wait, there's more! You have not copied it verbatim. You have renamed the file "module.c" to "assignment1.c", but neglected to change either the module name string or the init function string, and you have called your python file the same as your supposed module, "assignment1", which doesn't exist. A module called assignment1 should be in a library named assignment1.<your library extension> and have an init function named initassignment1 . The library you have created is not usable as a Python module. My guess is that your Python module assignment1.py has imported itself, and it of course doesn't have anything called sum .

Live demo of the fixed module .

All of this is totally irrelevant for calling C++ functions from Python. Just use pybind11.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM