Joe*_*Joe 5 c++ python swig ironpython cpython
我有一个简单的例子,我想从Ironpython访问(我来自'常规/理智'python')所以我正在努力将我的C++代码导入Ironpython.通常我只是使用SWIG,包装我的代码,导入并继续我的快乐方式
但是,由于Ironpython是C#而不是C语言,这使得这个过程变得更加困难
我如何为ironpython包装这个类(我还为此示例附加了我的swig文件,但这可能没用)
#include "minimal.h"
double average(std::vector<int> v) {
return std::accumulate(v.begin(), v.end(), 0.0) / v.size();
}
std::vector<double> half(const std::vector<double>& v) {
std::vector<double> w(v);
for (unsigned int i = 0; i<w.size(); i++)
w[i] /= 2.0;
return w;
}
void halve_in_place(std::vector<double>& v) {
std::transform(v.begin(), v.end(), v.begin(),
std::bind2nd(std::divides<double>(), 2.0));
}
Run Code Online (Sandbox Code Playgroud)
用头文件
#include <vector>
#include <algorithm>
#include <functional>
#include <numeric>
double average(std::vector<int> v);
std::vector<double> half(const std::vector<double>& v);
void halve_in_place(std::vector<double>& v);
Run Code Online (Sandbox Code Playgroud)
我有一个swig i文件minimal.i,但意识到有很多问题发出一个swig.exe -c ++ -python"%(FullPath)",并且铁蟒实际上在导入时接受它.
%module transfervector
%{
#include "minimal.h"
%}
%include "std_vector.i"
// Instantiate templates used by example
namespace std {
%template(IntVector) vector<int>;
%template(DoubleVector) vector<double>;
}
// Include the header file with above prototypes
%include "minimal.h"
Run Code Online (Sandbox Code Playgroud)
SWIG-python 不起作用 - 它会生成 CPython 扩展,而 IronPython 不支持这些扩展。
最终您需要包装 C++,以便可以从 .NET 访问它。我认为您可以使用 SWIG 生成 C# 包装器,然后可以将其导入到 IronPython 中。否则,您也许可以使用 C++/CLI 编译器进行编译,直接生成 .NET 程序集,也可以从 IronPython 使用该程序集。
| 归档时间: |
|
| 查看次数: |
416 次 |
| 最近记录: |