2016-10-03 1 views
1

私はSWIGのチュートリアルに従うことをしようとしているが、私は立ち往生持って、今私が使用している:SWIGチュートリアル問題

  • をPythonの3.5.1(V3.5.1:37a07cee5969、2015年12月6日、午前1時54分25秒)MSC v.1900 64ビット(AMD64)] Win32で
  • Vs2015のx64は、Microsoft(R)C/C++コンパイラの最適化バージョン19.00.23918 x64のため
  • SWIGバージョン3.0.10

内容は以下の通りです。

はexample.c

#include <time.h> 
double My_variable = 3.0; 

int fact(int n) { 
    if (n <= 1) return 1; 
    else return n*fact(n-1); 
} 

int my_mod(int x, int y) { 
    return (x%y); 
} 

char *get_time() 
{ 
    time_t ltime; 
    time(&ltime); 
    return ctime(&ltime); 
} 

example.i

%module example 
%{ 
/* Put header files here or function declarations like below */ 
extern double My_variable; 
extern int fact(int n); 
extern int my_mod(int x, int y); 
extern char *get_time(); 
%} 

extern double My_variable; 
extern int fact(int n); 
extern int my_mod(int x, int y); 
extern char *get_time(); 

それから私は実行します。

  • swig -python example.i
  • cl /D_USRDLL /D_WINDLL example.c example_wrap.c -Ic:\Python351\include /link /DLL /out:example.pyd /libpath:c:\python351\libs python35.lib

しかし、私はpython -c "import example"をしようとすると、私が手:

Traceback (most recent call last): 
    File "<string>", line 1, in <module> 
ImportError: dynamic module does not define module export function (PyInit_example) 

質問、何が起こっているとどのように私はそれを修正しますの?

答えて

1

SWIGのダイナミックリンクモジュールの名前は、アンダースコア(この場合は_example.pyd)で始まる必要があります。 SWIGは、そのファイルの先頭を参照して、Pythonのファイルは_exampleという名前のモジュールを探している生成:

from sys import version_info 
if version_info >= (2, 6, 0): 
    def swig_import_helper(): 
     from os.path import dirname 
     import imp 
     fp = None 
     try:           # ↓ SEE HERE 
      fp, pathname, description = imp.find_module('_example', [dirname(__file__)]) 
     except ImportError: 
      import _example # ← AND HERE 
      return _example # ← AND HERE 
     if fp is not None: 
      try:      # ↓ AND HERE 
       _mod = imp.load_module('_example', fp, pathname, description) 
      finally: 
       fp.close() 
      return _mod 
    _example = swig_import_helper() # ← AND HERE 
    del swig_import_helper 
else: # ↓ AND HERE 
    import _example 

実際、SWIGでラップされているC++モジュールの名前です。

関連する問題