2016-07-02 186 views
1

我需要为性能和CUDA利用率创建一个新的python模块作为c扩展。我已经尝试了几个教程,并没有成功。下面是我的文件:构建Python 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

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

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

所以,为了使你的“你好”模块编译你将不得不做出相应的改变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 file (在我的系统中,它被命名为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

+1

官方文档这个工作谢谢!我所要做的只是查看3.5文档来解决这个问题,所以这是我的错误。非常感谢帮助我! – Dan