Tôi đang cố gắng bọc một hàm foo trong test.cpp
với swig. Tôi có một tiêu đề foo.h
có chứa khai báo hàm foo. test.cpp
phụ thuộc vào tiêu đề bên ngoài ex.h
và tệp đối tượng được chia sẻ libex.so
nằm trong /usr/lib64
SWIG + setup.py: ImportError: mô-đun động không xác định hàm init (init_foo)
Tôi theo dõi blog post from here.
Tôi có thể tạo mô-đun với python setup.py build_ext --inplace
. Tuy nhiên khi tôi cố gắng để nhập nó tôi nhận được lỗi sau và tôi không chắc chắn những gì tôi đang mất tích như hầu hết những người khác câu hỏi với lỗi này không sử dụng một tập tin setup.py
. Dưới đây là một ví dụ về những gì tôi hiện có.
Các Lỗi trên nhập khẩu _foo:
>>> import _foo
ImportError: dynamic module does not define init function (init_foo)
test.i
%module foo
%{
#pragma warning(disable : 4996)
#define SWIG_FILE_WITH_INIT
#include "test.h"
%}
%include <std_vector.i>
%include <std_string.i>
%include "test.h"
test.cpp
#include "ex.h"
void foo(int i){
return;
};
test.h
#include "ex.h"
void foo(int i);
setup.py
try:
from setuptools.command.build_ext import build_ext
from setuptools import setup, Extension, Command
except:
from distutils.command.build_ext import build_ext
from distutils import setup, Extension, Command
foo_module = Extension('_foo',
sources=['foo.i' , 'foo.cpp'],
swig_opts=['-c++'],
library_dirs=['/usr/lib64'],
libraries=['ex'],
include_dirs = ['/usr/include'],
extra_compile_args = ['-DNDEBUG', '-DUNIX', '-D__UNIX', '-m64', '-fPIC', '-O2', '-w', '-fmessage-length=0'])
setup(name='mymodule',
ext_modules=[foo_module],
py_modules=["foo"],
)
bạn đang nhìn thấy nó tạo ra một file bọc biên dịch các tập tin được tạo ra và liên kết mà vào mô-đun? Một cái gì đó như 'swig-python -C++ -o foo_wrap.cpp foo.i',' gcc ... foo_wrap.cpp ... ',' g ++ ... foo_wrap.o ... 'như trong đầu ra trong blog post trích dẫn – Thomas
@Thomas Có một kết quả dài/tương tự tôi sẽ đăng những gì tôi thấy trong giây lát. – pyCthon
@Thomas đây là đầu ra, http://pastebin.com/MSChdNMy – pyCthon