【发布时间】:2019-04-06 18:55:50
【问题描述】:
我正在合作
-Windows 10 64 位
-Swig 3.0.12,我从http://www.swig.org/download.html下载的
-win32 上的 Python 3.5.4 [MSC v.1900 64 位 (AMD64)]
-MinGW7.3.0 64位
这是我第一次使用 swig 用 c++ 代码扩展我的 python 应用程序。我尝试编译了swig官网a link的示例代码。 Python 3.5 的 libs 文件夹中已经有了 libpython.a,这是用 mingw 编译时需要的,所以我使用了它。我按照a link 的教程进行操作!在 windows 中编译 swig。
具体来说,我运行以下
swig –python –c example.i
gcc -c *.c
gcc -c example_wrap.cxx -Ic:\python35\include
gcc -shared *.o -o _example.pyd -Lc:\python35\libs -lpython35
到这里,一切正常!
但是当我尝试在 Python35 中导入它时,我收到以下错误消息
Microsoft Windows [版本 10.0.17763.379] (c) 2018 年微软公司。保留所有权利。
蟒蛇
导入示例
example.fact(3)
Traceback(最近一次调用最后一次): 文件“”,第 1 行,在 TypeError:在方法“fact”中,参数 1 类型为“int”
哪里出错了?
/* File : example.c */
double My_variable = 3.0;
/* Compute factorial of n */
int fact(int n) {
if (n <= 1)
return 1;
else
return n*fact(n-1);
}
/* Compute n mod m */
int my_mod(int n, int m) {
return(n % m);
}
/* File : example.i */
%module example
%{
/* Put headers and other declarations here */
extern double My_variable;
extern int fact(int);
extern int my_mod(int n, int m);
%}
extern double My_variable;
extern int fact(int);
extern int my_mod(int n, int m);
# This file was automatically generated by SWIG (http://www.swig.org).
# Version 3.0.12
#
# Do not make changes to this file unless you know what you are doing--modify
# the SWIG interface file instead.
from sys import version_info as _swig_python_version_info
if _swig_python_version_info >= (2, 7, 0):
def swig_import_helper():
import importlib
pkg = __name__.rpartition('.')[0]
mname = '.'.join((pkg, '_example')).lstrip('.')
try:
return importlib.import_module(mname)
except ImportError:
return importlib.import_module('_example')
_example = swig_import_helper()
del swig_import_helper
elif _swig_python_version_info >= (2, 6, 0):
def swig_import_helper():
from os.path import dirname
import imp
fp = None
try:
fp, pathname, description = imp.find_module('_example', [dirname(__file__)])
except ImportError:
import _example
return _example
try:
_mod = imp.load_module('_example', fp, pathname, description)
finally:
if fp is not None:
fp.close()
return _mod
_example = swig_import_helper()
del swig_import_helper
else:
import _example
del _swig_python_version_info
try:
_swig_property = property
except NameError:
pass # Python < 2.2 doesn't have 'property'.
try:
import builtins as __builtin__
except ImportError:
import __builtin__
def _swig_setattr_nondynamic(self, class_type, name, value, static=1):
if (name == "thisown"):
return self.this.own(value)
if (name == "this"):
if type(value).__name__ == 'SwigPyObject':
self.__dict__[name] = value
return
method = class_type.__swig_setmethods__.get(name, None)
if method:
return method(self, value)
if (not static):
if _newclass:
object.__setattr__(self, name, value)
else:
self.__dict__[name] = value
else:
raise AttributeError("You cannot add attributes to %s" % self)
def _swig_setattr(self, class_type, name, value):
return _swig_setattr_nondynamic(self, class_type, name, value, 0)
def _swig_getattr(self, class_type, name):
if (name == "thisown"):
return self.this.own()
method = class_type.__swig_getmethods__.get(name, None)
if method:
return method(self)
raise AttributeError("'%s' object has no attribute '%s'" % (class_type.__name__, name))
def _swig_repr(self):
try:
strthis = "proxy of " + self.this.__repr__()
except __builtin__.Exception:
strthis = ""
return "<%s.%s; %s >" % (self.__class__.__module__, self.__class__.__name__, strthis,)
try:
_object = object
_newclass = 1
except __builtin__.Exception:
class _object:
pass
_newclass = 0
def fact(arg1):
return _example.fact(arg1)
fact = _example.fact
def my_mod(n, m):
return _example.my_mod(n, m)
my_mod = _example.my_mod
# This file is compatible with both classic and new-style classes.
cvar = _example.cvar
【问题讨论】:
-
example.i 和 example.c 是正确的。 gcc 看起来是正确的,但我认为它缺少
-fpic选项。我没有安装 MingGW,但该代码可与 Microsoft 编译器一起使用,并根据该编译器的需要添加了 __declspec(dllexport)。顺便说一句,没有 swig-c开关,所以我认为这是一个错字。 -
首先,谢谢。其次,您能否帮我写下您为在 msvs 中生成包装器而编写的命令。您使用哪个 python 和 msvs 版本?
-
我添加了一个答案。为了澄清我之前的评论,
__declspec(export)仅在您构建 C DLL 以及包装它的 Python 扩展时才需要。在下面的回答中,我将包装器和 C 代码链接在一起,因此不需要它。
标签: c++ python-3.x swig