python - 外部命令 C 和 Python

标签 python c

我正在尝试在 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/

相关文章:

python - 使用 CTRL+c 杀死一个 subprocess.Popen 子进程

python - os.walk() python : xml representation of a directory structure, 递归

c - unsigned char 到 8 个原始位的 unsigned char 数组

python - 比较两个列表,如果相等则替换第三个列表中的值

python - 可以将 Python 配置为缓存 sys.path 目录查找吗?

python - mustache /pystache : Rendering complex objects

c++ - 检测是否同时按下 2 个键

c - c-如果将hdc传递给方法,则不会绘制win32 api矩形

c - 将程序输出重定向到我的程序

c - 如何让gcc只生成可以直接加载到内存中执行的机器码?