gpt4 book ai didi

python - 自定义 C 模块包括

转载 作者:太空宇宙 更新时间:2023-11-04 04:21:04 25 4
gpt4 key购买 nike

我正在尝试使用 C 构建 Python 模块,但遇到了一个问题:

当我包含额外的头文件 (test.h) 时,模块编译时没有警告,但稍后在 import 这个模块时,Python 提示 undefined symbol 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

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

最佳答案

好的,谢谢 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 ])

关于python - 自定义 C 模块包括,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46800479/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com