python - 带有 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()
此命令没有输出...我的文件没有正确构建吗?
解决方案
所以看起来我必须添加“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 函数起作用了!
推荐阅读
- arrays - 如何引用数组变量中的所有元素
- javascript - 函数名称,“onButtonClick”或“onClickButton”
- c - C指针不等于另一个
- list - 我的问题是关于在 Dart 中构建不断增长的多维列表(数组)的方法
- linux - 如何使用 linux shell 选择文件中不同列的内容?
- android - 应用未设置:此应用仍处于开发模式,Facebook UI 更改
- excel - Excel 从非连续单元格中查找中位数并忽略 0 值
- angular - 在Angular7中将图像(来自外部库)保存到本地的PDF文件?
- jquery - 通过函数将`this`作为变量与jquery传递
- django - 模型可以有 ForeignKey 对象的列表吗?