我试图用swig将函数foo包装在test.cpp
中。我有一个 header foo.h
,其中包含函数foo的声明。 test.cpp
取决于外部 header ex.h
和位于libex.so
中的共享对象文件/usr/lib64
我遵循了blog post from here。
我可以使用python setup.py build_ext --inplace
构建模块。但是,当我尝试导入它时,出现以下错误,我不确定我丢失了什么,因为大多数其他与该错误有关的问题都不使用setup.py
文件。以下是我目前拥有的一个示例。
导入_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"],
)
最佳答案
看起来foo
和_foo
的使用存在一些不一致,因为包装文件是通过编译和链接生成的。
尝试从test.i
中更改模块名称
%module foo
至
%module _foo
或从以下位置调整
setup.py
中的扩展声明Extension('_foo',
至
Extension('foo',