自定义C模块包括

时间:2017-10-17 23:03:11

标签: python c python-2.7 cpython

我正在尝试使用C构建一个Python模块并坚持一个问题:

当我包含额外的头文件(test.h)时,模块编译没有警告,但稍后在import这个模块上,Python抱怨未定义的符号maketest

我的模块结构如下(模块名为spam):

spam\
    ├spammodule.c
    |setup.py
    ├─src\
         |── test.c
         |── test.h

spammodule.c的内容:

#include <Python.h>
#include "test.h"

static PyObject * SpamError;

static PyObject *
spam_system(PyObject *self, PyObject *args)
{
    const char *command;
    int sts;

    maketest(); // <---- calling function from additional file

    if(!PyArg_ParseTuple(args, "s", &command))
        return NULL;

    sts = system(command);
    if ( sts < 0 ) {
        PyErr_SetString(SpamError, "System command failed");
        return NULL;
    }
    return PyLong_FromLong(sts);
}

PyMODINIT_FUNC
initspam(void)
{
    PyObject *m;
    static PyMethodDef SpamMethods[] = {
        {"system", spam_system, METH_VARARGS, "Execute a shell command."},
        {NULL, NULL, 0, NULL}
    };
    m = Py_InitModule("spam", SpamMethods);
    if ( m == NULL )
        return;

    SpamError = PyErr_NewException("spam.error", NULL, NULL);
    Py_INCREF(SpamError);
    PyModule_AddObject(m, "error", SpamError);
}

int
main(int argc, char *argv[])
{
    Py_SetProgramName(argv[0]);
    Py_Initialize();
    initspam();
}

setupy.py的内容:

from setuptools import setup,Extension

spam_module = Extension('spam',
        sources = ['spammodule.c'],
        include_dirs=['src/'],)

setup ( name = 'Spam',
    version = '1.0',
    description = 'Sample module.',
    ext_modules = [ spam_module ])

src/test.h的内容:

void maketest(void);

src/test.c的内容:

#include "test.h"

void maketest() {
    printf("test passed");
}

我用python setup.py build编译所有内容,运行Python提示后我尝试导入我的模块并得到错误:

Python 2.7.10 (default, Oct 14 2015, 16:09:02)
[GCC 5.2.1 20151010] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import spam
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: ./spam.so: undefined symbol: maketest

有人知道发生了什么事吗?

1 个答案:

答案 0 :(得分:0)

好的家伙感谢 Antii 提供线索,因为您说问题是test.c应该包含在源元组中 所以我修改了setup.py,一切正常 现在它看起来像这样:

from setuptools import setup,Extension

spam_module = Extension('spam',
        sources = ['spammodule.c','src/test.c'],
        include_dirs=['src/'])

setup ( name = 'Spam',
    version = '1.0',
    description = 'Sample module.',
    ext_modules = [ spam_module ])