首页 > 解决方案 > 带有 C++ 的简单 SWIG 用于 python 编译,但输出似乎不正确?

问题描述

我对 SWIG(和 c++)相当陌生,所以我试图使用这个网站上的一个例子,这 似乎是一个简单的代码。我在 Visual Studio 上编译它......成功(我认为?)。

1>------ Rebuild All started: Project: minimal, Configuration: Release x64 ------
1>Performing Custom Build Tools
1>minimal.cpp
1>minimal_wrap.cxx
1>   Creating library ..._minimal2.lib and object ...minimal2.exp
1>Generating code
1>Previous IPDB not found, fall back to full compilation.
1>All 70 functions were compiled because no usable IPDB/IOBJ from previous compilation was found.
1>Finished generating code
1>minimal.vcxproj -> C:\...\_minimal.pyd
========== Rebuild All: 1 succeeded, 0 failed, 0 skipped ==========

但是当我尝试在 spyder (ipython 控制台)上运行它时

import _minimal
m1 = minimal.minimal()
print(m1)

输出<minimal.minimal; proxy of <Swig Object of type 'minimal2 *' at 0x000001D0C68BDDE0>

从教程网站上,听起来我应该得到一些整数?

m1.print_num_instances()

此命令没有输出...我的文件没有正确构建吗?

标签: pythonc++visual-studiospyderswig

解决方案


所以看起来我必须添加“typemaps.i”并定义输出类型。我没有处理上述问题中使用的示例,但我发现另一个示例非常有用。

/* typemaps.i allows input and output pointer arguments to be specified using the names INPUT, OUTPUT, or INOUT */
%include "typemaps.i"

void   hw1(double r1, double r2, double *OUTPUT);

该示例显示了他们的第一次尝试以我得到的类似指针类型的响应失败。然后在 typeamps.i 之后,它的 hw1 函数起作用了!


推荐阅读