问题描述
我的问题如下:
我想从我的Python文件调用C函数并返回一个值回了Python文件。
我曾尝试使用Python中嵌入C(以下code是所谓的mod1.c的C code)。我使用Python3.4所以格式遵循的指导文件给出了下面的方法。问题是当我把我的安装文件(下图2 code)。
#包括
#包括sum.h
My problem is as follows: I would like to call a C function from my Python file and return a value back to that Python file. I have tried the following method of using embedded C in Python (the following code is the C code called "mod1.c). I am using Python3.4 so the format follows that given in the documentation guidelines. The problem comes when I call my setup file (second code below). #include #include "sum.h"
static PyObject*
mod_sum(PyObject *self, PyObject *args)
{
int a;
int b;
int s;
if (!PyArg_ParseTuple(args,"ii",&a,&b))
return NULL;
s = sum(a,b);
return Py_BuildValue("i",s);
}
/* DECLARATION OF METHODS */
static PyMethodDef ModMethods[] = {
{"sum", mod_sum, METH_VARARGS, "Descirption"}, // {"methName", modName_methName, METH_VARARGS, "Description.."}, modName is name of module and methName is name of method
{NULL,NULL,0,NULL}
};
// Module Definition Structure
static struct PyModuleDef summodule = {
PyModuleDef_HEAD_INIT,
"sum",
NULL,
-1,
ModMethods
};
/* INITIALIZATION FUNCTION */
PyMODINIT_FUNC initmod(void)
{
PyObject *m;
m = PyModule_Create(&summodule);
if (m == NULL)
return m;
}
Setup.py
从distutils.core进口设置,扩展
Setup.py from distutils.core import setup, Extension
setup(name='buildsum', version='1.0', \
ext_modules=[Extension('buildsum', ['mod1.c'])])
当我使用的gcc以下错误编译我的code,我得到的结果是:的无法导出PyInit_buildsum:没有定义的符号的
The result that I get when I compile my code using gcc is the following error: Cannot export PyInit_buildsum: symbol not defined
我将不胜AP preciate任何见解或帮助解决这个问题,还是如何从Python中调用C的任何建议。谢谢!
I would greatly appreciate any insight or help on this problem, or any suggestion in how to call C from Python. Thank you!
---------------------------------------编辑------- --------------------------
谢谢你的评论:
我曾尝试以下现在:
---------------------------------------EDIT ---------------------------------Thank you for the comments: I have tried the following now:
static PyObject*
PyInit_sum(PyObject *self, PyObject *args)
{
int a;
int b;
int s;
if (!PyArg_ParseTuple(args,"ii",&a,&b))
return NULL;
s = sum(a,b);
return Py_BuildValue("i",s);
}
有关的第一功能;不过,我仍然得到同样的错误 PyInit_sum:没有定义符号的
For the first function; however, I still get the same error of PyInit_sum: symbol not defined
推荐答案
从上面的工作code的情况下,任何人都跑进了同样的错误:从@dclarke的答案是正确的。在Python 3初始化函数必须PyInit_(名)作为其名称。
The working code from above in case anyone runs into the same error: the answer from @dclarke is correct. The initialization function in python 3 must have PyInit_(name) as its name.
#include <Python.h>
#include "sum.h"
static PyObject* mod_sum(PyObject *self, PyObject *args)
{
int a;
int b;
int s;
if (!PyArg_ParseTuple(args,"ii",&a,&b))
return NULL;
s = sum(a,b);
return Py_BuildValue("i",s);
}
/* DECLARATION OF METHODS*/
static PyMethodDef ModMethods[] = {
{"modsum", mod_sum, METH_VARARGS, "Descirption"},
{NULL,NULL,0,NULL}
};
// Module Definition Structure
static struct PyModuleDef summodule = {
PyModuleDef_HEAD_INIT,"modsum", NULL, -1, ModMethods
};
/* INITIALIZATION FUNCTION*/
PyMODINIT_FUNC PyInit_sum(void)
{
PyObject *m;
m = PyModule_Create(&summodule);
return m;
}
这篇关于从调用一个Python文件中的C函数。使用Setup.py文件时收到错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!