0

我在 mac OS X 10.10.2 上使用 Python3。我是 python C-API 的新手,所以我正在尝试来自“Python Cookbook” http://chimera.labs.oreilly.com/books/1230000000393/ch15.html的 C-API 扩展的简单示例。我认为 Python2 和 Python3 之间存在一些差异。我遵循了 Python3 文档,但找不到我的案例不起作用的原因。

直到用 distills 构建(python3 setup.py buildpython3 setup.py build_ext --inplace)给我sample.so文件。我也编译sample.csample.o.

但是,当我从 python 导入时,我收到如下所示的错误消息,我不明白发生了什么。下面显示了在 ipython3 中运行的错误消息。

请帮助我解决扩展模块的问题。谢谢!

In [1]: import sample
---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-6-a769eab12f54> in <module>()
----> 1 import sample

ImportError: dlopen(/Users/suh/pyLNKS/python-api-example/sample/sample.so, 2): Symbol not found: _gcd
Referenced from: /Users/suh/pyLNKS/python-api-example/sample/sample.so
Expected in: flat namespace
in /Users/suh/pyLNKS/python-api-example/sample/sample.so

In [2]:

以下是文件pysample.csample.csample.h和的代码setup.py

pysample.c

1 #include <Python.h>
2 #include "sample.h"
3 
4 
5 static PyObject *py_gcd(PyObject *self, PyObject *args) {
6     int x, y, result;
7 
8     if (!PyArg_ParseTuple(args, "ii", &x, &y)) {
9         return NULL;
10     }
11     result = gcd(x, y);
12     return Py_BuildValue("i", result);
13 }
14 
15 
16 /* module method table */
17 static PyMethodDef SampleMethods[] = {
18     {"gcd", py_gcd, METH_VARARGS, "Greatest commond divisor"},
19    // {"divide", py_divide, METH_VARARGS, "Integer division"},
20     {NULL, NULL, 0, NULL}
21 };
22 
23 static struct PyModuleDef samplemodule = {
24     PyModuleDef_HEAD_INIT,
25     "samle",    /* name of module */
26     "A sample module",  /* doc string, may be NULL */
27     -1, /* size of per-interpreter state of the module, 
28            or -1 if the module keeps state in global variables */
29     SampleMethods   /* methods table */
30 };  
31 
32 PyMODINIT_FUNC
33 PyInit_sample(void){
34     return PyModule_Create(&samplemodule);
35 }   
36     

样本.c

1 /* sample.c */
2 #include <math.h>
3 
4 /* compute the greatest common divisor */
5 int gcd(int x, int y){
6     int g = y;
7     while (x > 0){
8         g = x;
9         x = y % x;
10         y = g;
11     }
12     return g;
13 }

样本.h

1 /* sample.h */
2 #include <math.h>
3 
4 extern int gcd(int x, int y);

安装程序.py

1 # setup.py
2 from distutils.core import setup, Extension
3 
4 module1 = Extension('sample', sources = ['pysample.c'])
5 
6 setup(name='simple',
7         version = '1.0',
8         description = "this is a demo package",
9         ext_modules = [module1]
10         )
11 
4

2 回答 2

0

There is a typo in the samplemodule structure. The module name is "sample", not "samle"

于 2015-02-05T08:34:46.783 回答
0

存在链接问题,源代码 sample.c 未链接,因此 gcd 函数未定义。

因此,需要将代码 setup.py 更改为:

1 # setup.py
2 from distutils.core import setup, Extension
3 
4 module1 = Extension('sample', sources = ['pysample.c','source.c'])
5 
6 setup(name='simple',
7         version = '1.0',
8         description = "this is a demo package",
9         ext_modules = [module1]
10         )
11    
于 2015-02-05T08:17:01.293 回答