【发布时间】:2016-06-27 03:44:25
【问题描述】:
我正在尝试用 swig 将函数 foo 包装在 test.cpp 中。我有一个标题foo.h,其中包含函数 foo 的声明。 test.cpp 依赖于外部标头ex.h 和位于/usr/lib64 中的共享对象文件libex.so
我关注了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"],
)
【问题讨论】:
-
您是否看到它生成一个包装文件,编译生成的文件并将其链接到模块中?类似于
swig -python -c++ -o foo_wrap.cpp foo.i、gcc ... foo_wrap.cpp ...、g++ ... foo_wrap.o ...之类的内容,如引用的博文中的输出 -
@Thomas 是的,有一个很长/类似的输出,我稍后会发布我看到的内容。
-
@Thomas 这是输出,pastebin.com/MSChdNMy
-
嗯,看起来不错。
foo_wrap.cpp中是否有init_foo方法?_foo.so中是否有init_foo符号? -
@Thomas 回答一下,我可以奖励你奖金
标签: python c++ linux python-2.7 swig