环境:ubuntu,x64;jupyter-lab;jupyter-lab中安装boost库.
目的:在python环境下,输入数据到C++的函数,C++函数返回数组,供python调用.
C++的数组可以用python的numpy.narry表示,但是由于版本问题,调用boost::python::numpy时会提示无此库,需要安装特定的boost库.故本次采用boost::python::list的方式保存C++的输出数组.下面显示例子.
1,编写tests.cpp,主要用MyFunc函数调用.此处注意返回为指针,如果在函数内部定义c数组,后期运行时会因为局部变量导致无法传出参数,故此处将c定义在函数输入.
//tests.cpp
#include "tests.h"
#include <iostream>
using namespace std;
double* MyFunc(double* c,double a,double b) {
// double c[4]; //如果此处定义c,则后期调用会出现局部变量无法传参问题
c[0]=a;
c[1]=b;
c[2]=a+b;
c[3]=a-b;
return c;
}
2,与上面test.cpp对应,编写h函数声明.
//tests.h
double* MyFunc(double*,double,double);//不能有变量,只进行类型声明
3,python调用c++,为方便管理,编写一个接口文件,方便调用.此处命名为pyc.cpp,如下所示,通过建立类,调用上述函数输入输出.c为函数输出数组.通过list传出.
//pyc.cpp
#include <boost/python.hpp>
#include <stddef.h>
#include <stdio.h>
#include "tests.h"
#include <iostream>
#include <sstream>
#include <string>
using namespace boost::python;
// 定义一个类
class cdescri
{
public:
cdescri(){this->privateVal=333;} //默认构造函数
cdescri(const std::string& Nums){
this->runNum = Nums;
} //带参数的构造函数
void set(int val){privateVal=val;} //成员函数
int get() const {return this->privateVal;} //成员函数
std::string runNum;
void set_a(double ia){this->a=ia;}
void set_b(double ib){this->b=ib;}
boost::python::list get_c(){
boost::python::list func;
MyFunc(c,this->a,this->b); //输出
for (int i = 0; i <4; ++i){ func.append(c[i]); }
return func;}
private:
int privateVal; //私有数据成员
double a;
double b;
double c[4];
boost::python::list func;
};
BOOST_PYTHON_MODULE(pyc) //注意此名字与此cpp文件名一致,输入输出接口函数
{
using namespace boost::python;
boost::python::class_<cdescri>("cdescri", init<std::string>())
//类名,默认构造函数
.def(init<std::string>())
//其他构造函数
.def("get", &cdescri::get)
.def("set", &cdescri::set)
.def("set_a", &cdescri::set_a)
.def("set_b", &cdescri::set_b)
.def("get_c",&cdescri::get_c)
;
}
上述三个文件,编写Makefile文件.-I和-L分别为编译的python版本位置和boost位置
pyc.so : pyc.cpp tests.h tests.cpp
g++ pyc.cpp -fPIC -shared -o pyc.so tests.h tests.cpp -I. -I/usr/include/python3.6m -I/usr/local/include/boost -L/usr/local/lib -lboost_python3
clean:
rm -rf pyc.so *.o *.so
执行Makefile文件,编译生成.so.可以在终端完成或者在jupyter中完成.此处在jupyter中完成.
!make clean
!make
如果没报错,则在此目录下会生成pyc.so文件.则可以直接在jupyter中调用此模块.调用形式如下.直接赋值后
import pyc
Btest=pyc.cdescri('test1')
Btest.set_a(1)
Btest.set_b(2)
testc = Btest.get_c()
print('testc:',testc)
输出为list
[1.0, 2.0, 3.0, -1.0]