栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

生成c++与Python的vector接口

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

生成c++与Python的vector接口

安装完Swig后,需要编辑两个文件,即example.i和example.cpp

其中example.i为:

%module example_vector

%include "std_string.i"
%include "std_vector.i"

%{
using namespace std;

vector vector_int2str(vector input);
%}

namespace std {
  %template(StringVector) vector;
  %template(IntVector) vector;
}

using namespace std;

vector vector_int2str(vector input);

其中example.cpp为:

#include 
#include 

using namespace std;

vector vector_int2str(vector input) {
    vector result;
    for (vector::const_iterator it = input.begin();
        it != input.end();
        ++it) {
        result.push_back(to_string(*it));
    }
    return result;
}

生成后使用conda进入python2环境,以此执行下面的编译命令

swig -c++ -python example.i

g++ -fPIC -c example.cpp example_wrap.cxx -I/home/l/miniconda3/envs/python2/include/python2.7

g++ -shared example.o example_wrap.o -o _example_vector.so

其中-I/home/l/miniconda3/envs/python2/include/python2.7是python的安装目录,可以进入conda环境后使用下面的命令查找

python-config --cflags
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/664947.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号