我正在尝试在 C 和 Python 集成中调用外部变量 cwdata。这是我的文件 randomGoppa.c
extern precomp_t cwdata;
static PyObject* dicho(PyObject* self, PyObject* args){
unsigned char * c;
int * e;
PyArg_ParseTuple(args, "ii", &c, &e);
int i = dicho_b2cw(cwdata);
}
static PyMethodDef randomGoppa_methods[] = {{ "dicho",dicho, METH_VARARGS},
{NULL, NULL}};
void initrandomGoppa() {
(void) Py_InitModule("randomGoppa", randomGoppa_methods);
}
当我使用
编译时gcc -shared -fPIC -I/usr/include/python2.7/ -lpython2.7 -o randomGoppa.so randomGoppa.c mat.c poly.c gf.c dicho.c
是不是有错误。但是当我在 python 中尝试导入时出现错误
from randomGoppa import *
lib/python2.7/site-packages/randomGoppa.so: undefined symbol: cwdata
有人可以解释一下这里发生了什么吗?
最佳答案
您已使用 extern
声明了 cwdata
。这会导致编译器不为变量分配空间,这就是它未定义的原因。 (编译器希望在将来链接的不同模块中定义该变量。)由于您打算在该 C 文件中定义 cwdata
,因此只需删除 extern
> 关键字。
关于python - 外部命令 C 和 Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25286686/