2016-07-02 10 views
1

パフォーマンスとCUDAの両方の使用のために、新しいPythonモジュールをc拡張として作成する必要があります。私はこのためにいくつかのチュートリアルを試して失敗しました。ここに私のファイルは、以下のとおりです。Python C extensionのビルド

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です。右inithello()機能上記struct

まず、追加します:

だから、あなたの「ハロー」モジュールを作成するためにあなたがhellomodule.cに適切な変更を加える必要がありますコンパイル

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:\> 

を、あなたはこれがあなたに感謝のPython 3.5

+1

ための公式ドキュメントにgo働いたことができます!私がしなければならなかったことは、これを理解するための3.5のドキュメントを見ることでした。それは私の悪いことです。これを手伝ってくれてありがとう! – Dan