boost.python编辑,以及c++api的python封装

boost.python 编辑与踩坑

踩坑
1、编辑的版本使用的vs版本不同的话,使用的命令不同
2、编辑第一条命令b2 toolset=msvc-11.0 --with-python
报错:fatal error c1083:无法打开inttypes.h文件
在网上查这个文件是vs自带的,因此可以直接去下载
下一步
3、link即可,下面附上完整的boost.python操作文档

Windows平台编译 Boost.Python
1. Boost 下载
链接:https://dl.bintray.com/boostorg/release/;
这里下载了boost_1_69_0;
2. 解压,进入编译环境
解压缩后,通过VS Build tools (或通过 VS2012-工具-VisualStudio
命令提示)进入 boost库 ,进行编译的步骤:
將目录cd到 boost_1_69_0 下执行
3. python首先确保 python 是否存在
键入 python(或者 where python) ,确保可以执行到需要编译的
python版本;
c:\boost_1_69_0>where python
C:\Python37\python.exe
4. 然后执行 bootstrap.bat
4.1 选择参数
可选 需要编译的编译器版本;
Visual Studio 2012 : vc11
Visual Studio 2013 : vc12
Visual Studio 2015 : vc14
我们使用 VS2012 即可以编译v110版本;
bootstrap.bat
或
bootstrap.bat vc11
4.2 执行 bootstrap.bat 例子:
c:\boost_1_69_0>bootstrap.bat vc11
Building Boost.Build engine
Bootstrapping is done. To build, run:
.\b2
To adjust configuration, edit 'project-config.jam'.
Further information:
- Command line help:
.\b2 --help
- Getting started guide:
http://boost.org/more/getting_started/windows.html
- Boost.Build documentation:
http://www.boost.org/build/doc/html/index.html
4.3 文件查看
执行  bootstrap.bat
成功执行后,会在boost根目录下生成 bootstrap.log 、 b2.exe 、
bjam.exe 。
其中可查看  bootstrap.log 查看运行结果;
bjam.exe 和 b2.exe 两个作用一样,
bjam.exe 对应的是老版本, b2 是 bjam 的升级版本。
5. 编译boost.python:
b2 toolset=msvc-11.0 --with-python
5.1 可选参数:
--toolset=msvc-11.0
指编译器为vc110的VS2012;
--with-python
只编译指定的库,如输入--with-python就只编译python库了
link
生成动态链接库(=static)/静态链接库(=shared)
runtime-link
动态/静态链接C++运行库,有shared和static两种方式
threading=multi
单/多线程编译,一般写多线程,直接指定为multi
--build-type=complete
编译所有版本
5.2 可以通过  b2 --help 查看更多参数;
例子:
c:\boost_1_69_0>b2 --help
Boost.Build 2018.02-git
Usage:
b2 [options] [properties] [install|stage]
(此处省略...)
--with-<library> Build and install the specified <library>.
option is used, only libraries specified
option will be built.
Properties:
toolset=toolset Indicate the toolset to build with.
link=static|shared Whether to build static or shared libraries
threading=single|multi Whether to build single or multithreaded bi
(此处省略...)
6. 查看编译结果:
编译的过程会比较长,最后会在boost目录下面生成两个文件夹
stage : 对应的lib和dll文件
bin.v2 : 编译时的中间文件,可以直接删除掉
可查看到已经编译好的python37 boost库;
boost_1_69_0/stage/lib$ ls -lrt
-rw-r--r-- 1 26583808 四月 25 15:18 libboost_python37-vc110-mt-gd-x32
-rw-r--r-- 1 6258968 四月 25 15:18 libboost_numpy37-vc110-mt-gd-x32-
-rw-r--r-- 1 6121450 四月 25 15:19 libboost_python37-vc110-mt-x32-1_
-rw-r--r-- 1 830718 四月 25 15:20 libboost_numpy37-vc110-mt-x32-1_6
-rw-r--r-- 1 31016626 四月 25 15:21 libboost_python37-vc110-mt-gd-x64
-rw-r--r-- 1 6717140 四月 25 15:21 libboost_numpy37-vc110-mt-gd-x64-
-rw-r--r-- 1 7392556 四月 25 15:22 libboost_python37-vc110-mt-x64-1_
-rw-r--r-- 1 943414 四月 25 15:23 libboost_numpy37-vc110-mt-x64-1_6

然后讲讲我封装的简单步骤:

API封装步骤
API封装大体分为三部分:主动请求函数、回调函数、封装回调内容函数
主动请求函数:python主动发送请求到c++的函数
回调函数:根据某主动请求函数回调某c++函数的函数
封装回调内容函数:将c++中回调函数处理后的内容使用boost库进行封装,形成python可以调用的形式。
主动请求函数封装:python脚本引用c++api模块,创建一个api对象调用c++非静态成员函数,利用boost库将主动请求函数和c++里面对应的函数绑定,根据绑定的c++函数将python的内容解析成c++可识别的。
回调函数封装:this指针将主动请求的内容发到父类里进行处理后,将处理后的内容返回将该内容塞到队列里,根据工作线程从队列里取数据,转化成python对象。
封装回调函数:使用boost库将在队列里转换成python对象的数据进行封装一遍,然后调用python的回调函数。
(注意:所有的主动函数、回调函数都是继承父类的)

 

posted @ 2019-07-16 18:15  枯藤呀~  阅读(622)  评论(0编辑  收藏  举报