2016-12-10 1 views
0

Ich befolge das Tutorial für SWIG and Python, aber ich erhalte einen Fehler, wenn ich versuche, die kompilierte Erweiterung in Python zu importieren.SWIG Symbol nicht gefunden, führender Unterstrich in Symbolname

Python 3.5.2 (default, Oct 11 2016, 15:01:25) 
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.38)] on darwin 

>>> import _example 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
ImportError: dlopen(/pat/to/_example.cpython-35m-darwin.so, 2): Symbol not found: _fact 
    Referenced from: /path/to/_example.cpython-35m-darwin.so 
    Expected in: flat namespace 
in /path/to/_example.cpython-35m-darwin.so 

Beachten Sie, dass der Fehler besagt, dass das Symbol _fact nicht gefunden werden kann. Ich bin mir nicht sicher, woher der führende Unterstrich kommt, und ich kann keine Informationen darüber finden, wie dieses Problem gelöst werden kann.

Hier ist example.i (dem entspricht, was im tutorial ist):

%module example 

%{ 
#define SWIG_FILE_WITH_INIT 
#include "example.h" 
%} 

int fact(int n); 

example.h und example.c passen auch das, was in der tutorial ist; Ich kann hier bei Bedarf posten.

Hier setup.py:

# coding=utf-8 
from setuptools import Extension, setup 

setup(
    name = 'SWIG Test', 
    ext_modules = [Extension('_example', ['example.i'])], 
    py_modules = ['example'], 
) 

Dies ist der entsprechende Ausgang von pip install -e . -v:

running build_ext 
building '_example' extension 
swigging example.i to example_wrap.c 
swig -python -o example_wrap.c example.i 
creating build 
creating build/temp.macosx-10.12-x86_64-3.5 
/usr/bin/clang -Wno-unused-result -Wsign-compare -Wunreachable-code -fno-common -dynamic -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -pipe -Os -I/opt/local/Library/Frameworks/Python.framework/Versions/3.5/include/python3.5m -c example_wrap.c -o build/temp.macosx-10.12-x86_64-3.5/example_wrap.o 
creating build/lib.macosx-10.12-x86_64-3.5 
/usr/bin/clang -bundle -undefined dynamic_lookup -L/opt/local/lib -Wl,-headerpad_max_install_names build/temp.macosx-10.12-x86_64-3.5/example_wrap.o -o build/lib.macosx-10.12-x86_64-3.5/_example.cpython-35m-darwin.so 
copying build/lib.macosx-10.12-x86_64-3.5/_example.cpython-35m-darwin.so -> 

einige Dinge, die ich versucht habe, die nicht funktionierten:

  • Deklarieren fact als int fact(int n) asm("fact") (nach dem Bau bekomme ich Symbol not found: fact beim Import).
  • Hinzufügen swig_opts=['-py3'] zu der Erweiterung in setup.py.

Antwort

1

Der führende Unterstrich war ein red herring. Stellt sich heraus, ich habe einfach vergessen, example.c in meine Erweiterungsquellen aufzunehmen.

Hier ist, was setup.py soll so aussehen:

# coding=utf-8 
from setuptools import Extension, setup 

setup(
    name = 'SWIG Test', 
    ext_modules = [Extension('_example', ['example.i', 'example.c'])], 
    py_modules = ['example'], 
) 

Beachten Sie, dass die Erweiterung sowohl example.i hat und example.c in den Quellen.

Jetzt funktioniert alles wie erwartet (:

Python 3.5.2 (default, Oct 11 2016, 15:01:25) 
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.38)] on darwin 

>>> import example 
>>> example.fact(42) 
0 
>>> example.fact(0) 
1 
Verwandte Themen