简体   繁体   English

从Python(Anaconda)调用C ++函数

[英]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? 所以我已经按照第4.1节中使用setup.py文档编译了我的C ++代码,似乎该模块在我查询assignment1.add?时已成功导入assignment1.add? after importing I receive the information: 导入后,我收到以下信息:

Docstring: Add two numbers. Docstring:加两个数字。

Type: builtin_function_or_method 类型: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". 但是,当我实际调用函数assignment1.sum(1,2) ,Python内核立即死亡,没有比“内核死亡,重新启动”更多的错误消息。

#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. 您的首次尝试使用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. 我不确定“链接器错误1120”是什么,所以不会查找它,但是我的猜测是未定义的引用,如果您尝试使用Python2.7(为什么呢?),这是完全可以理解的。Python2和Python3具有不同的不兼容C API。

The second attempt uses Python2.7. 第二次尝试使用Python2.7。 The example you have copied is wrong. 您复制的示例是错误的。 The init function to initialise a module named module should be called initmodule , not initmod . 用于初始化名为module的模块的init函数应称为initmodule ,而不是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. 您已将文件“ module.c”重命名为“ assignment1.c”,但忽略了更改模块名称字符串或init函数字符串的操作, 并且已将您的python文件称为假定的模块“ assignment1”,不存在。 A module called assignment1 should be in a library named assignment1.<your library extension> and have an init function named initassignment1 . 名为assignment1的模块应位于名为assignment1.<your library extension>并具有一个名为initassignment1的初始化函数。 The library you have created is not usable as a Python module. 您创建的库不能用作Python模块。 My guess is that your Python module assignment1.py has imported itself, and it of course doesn't have anything called sum . 我的猜测是您的Python模块assignment1.py已导入自身,当然它没有任何名为sum东西。

Live demo of the fixed module . 固定模块的现场演示

All of this is totally irrelevant for calling C++ functions from Python. 所有这些与从Python调用C ++函数完全无关。 Just use pybind11. 只需使用pybind11。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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