构建Python C扩展

时间:2016-07-02 02:39:04

标签: python c windows build compiler-errors

我需要创建一个新的python模块作为性能和CUDA利用率的c扩展。我已经为此尝试了几个教程并且没有成功。这是我的文件:

hellomodule.c

#include <Python.h>

static PyObject* say_hello(PyObject* self, PyObject* args)
{
    const char* name;

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

    printf("Hello %s!\n", name);

    Py_RETURN_NONE;
}

static PyMethodDef HelloMethods[] =
{
     {"say_hello", say_hello, METH_VARARGS, "Greet somebody."},
     {NULL, NULL, 0, NULL}
};

PyMODINIT_FUNC inithello(void)
{
     (void) Py_InitModule("hello", HelloMethods);
}

setuphello.py

from distutils.core import setup, Extension

module1 = Extension('hello', sources = ['hellomodule.c'])

setup (name = 'PackageName',
    version = '1.0',
        description = 'This is a demo package',
        ext_modules = [module1])

以下是python setuphello.py build的结果:

running build
running build_ext
building 'hello' extension
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\BIN\cl.exe /c /nologo /Ox /W3 /GL /DNDEBUG /MD -IC:\Anaconda3\include -IC:\Anaconda3\include "-IC:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\INCLUDE" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.10240.0\ucrt" "-IC:\Program Files (x86)\Windows Kits\NETFXSDK\4.6.1\include\um" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.10240.0\shared" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.10240.0\um" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.10240.0\winrt" "-IC:\Program Files (x86)\IntelSWTools\Trace Analyzer and Collector\9.1.2.024\include" /Tchellomodule.c /Fobuild\temp.win32-3.5\Release\hellomodule.obj
hellomodule.c
hellomodule.c(23): warning C4013: 'Py_InitModule' undefined; assuming extern returning int
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\BIN\link.exe /nologo /INCREMENTAL:NO /LTCG /DLL /MANIFEST:EMBED,ID=2 /MANIFESTUAC:NO /LIBPATH:C:\Anaconda3\libs /LIBPATH:C:\Anaconda3\PCbuild\win32 "/LIBPATH:C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\LIB" "/LIBPATH:C:\Program Files (x86)\Windows Kits\10\lib\10.0.10240.0\ucrt\x86" "/LIBPATH:C:\Program Files (x86)\Windows Kits\NETFXSDK\4.6.1\lib\um\x86" "/LIBPATH:C:\Program Files (x86)\Windows Kits\10\lib\10.0.10240.0\um\x86" /EXPORT:PyInit_hello build\temp.win32-3.5\Release\hellomodule.obj /OUT:build\lib.win32-3.5\hello.cp35-win32.pyd /IMPLIB:build\temp.win32-3.5\Release\hello.cp35-win32.lib
LINK : error LNK2001: unresolved external symbol PyInit_hello
build\temp.win32-3.5\Release\hello.cp35-win32.lib : fatal error LNK1120: 1 unresolved externals
error: command 'C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\VC\\BIN\\link.exe' failed with exit status 1120

我已经查看了其他人遇到的各种错误,并尝试按照他们的调试逻辑进行操作,但实际上我不知道幕后发生的错误是什么。我正在使用Python 3.5 32位(Anaconda),因此一直在尝试使用Visual C ++构建工具及其打包终端进行编译。然而,这没有任何区别。有人可以带领我朝着正确的方向前进吗?

1 个答案:

答案 0 :(得分:1)

您的方法存在的问题是,在使用Python 3.5.x解释器时,您依赖于Python 2.7.x的教程/指南(可能是this?)。

构建C扩展的方法在Python 3.x中有changed

所以,为了让你的'#hello&#39;模块编译你必须在 hellomodule.c 中进行适当的更改:

首先,在struct函数正上方添加以下inithello()

static struct PyModuleDef hellomodule = {
    PyModuleDef_HEAD_INIT,
    "hello", /* module name */
    NULL, /* module documentation, may be NULL */
    -1,
    HelloMethods /* the methods array */
};

然后用这个替换整个inithello()函数:

PyMODINIT_FUNC PyInit_hello(void)
{
    return PyModule_Create(&hellomodule);
}

您无需对 setuphello.py 脚本进行任何更改,您可以照常运行该脚本:

python setuphello.py build

您可以通过进入 build \ lib.win32-3.5 目录(或类似的东西)快速测试新编译的模块,复制 .pyd 文件(在我的系统中它名为 hello.cp35-win32.pyd )在某个地方方便,并使用类似这个小脚本的东西( usehello.py ):

import hello

def greet(person):
    hello.say_hello(person)

greet("stranger")

运行收益率:

c:\>python usehello.py
Hello stranger!

c:\>

有关扩展程序的完整说明,您可以go获取Python 3.5的官方文档