C++ boost.python折腾笔记
为了让当年研究生时写的图像处理系统重出江湖起到更大的作用,应研究生导师的意见,对原有的c++框架做了python扩展处理,为了避免遗忘,备注如下:
一、boost 编译
下载boost源码,这里使用boost 1.67,解压到目录,进行编译
- 下载C++的boost库:http://www.boost.org/
- 安装Anaconda3-5.1.0-Windows-x86_64 默认路径安装
- 解压boost文件,在其目录中执行
.\bootstrap.bat,会生成编译器b2.exe和bjam.exe - 修改
project-config.jam文件,加入python的版本及路径(不加入则会默认python2): - import option ;
using msvc ;
option.set keep-going : false ;
using python
: 3.6 # Version
: C:\\ProgramData\\Anaconda3\\python.exe # Python Path
: C:\\ProgramData\\Anaconda3\\include # include path
: C:\\ProgramData\\Anaconda3\\libs # lib path(s)
;- 执行命令(我这里是vs 2010 故为msvc-10.0)
`.\bjam.exe toolset=msvc-10.0 --with-python threading=multi link=shared address-model=64,在stage\lib目录中会生成boost_numpy3-*和boost_python3-*字样的文件 - 编译过程遇到了以下问题
- (1)缺少头文件 无法打开包括文件:“inttypes.h
#include<inttypes.h>
编译时,找不到此文件,所以无法打开
方法:
1. 获取此文件
2. 放置此文件到目录:
VS2008,C:\Program Files\Microsoft Visual Studio 9.0\VC\include
VS2010,C:\Program Files\Microsoft Visual Studio 10.0\VC\include- (2)boost 1.67的bug 找不到库
- C:\Boost\include\boost-1_67\boost\python\numpy\config.hpp
- 修改这一段
// enable automatic library variant selection ------------------------------//
#if !defined(BOOST_NUMPY_SOURCE) && !defined(BOOST_ALL_NO_LIB) && !defined(BOOST_NUMPY_NO_LIB)
//
// Set the name of our library, this will get undef'ed by auto_link.hpp
// once it's done with it:
//
//#define BOOST_LIB_NAME boost_numpy##PY_MAJOR_VERSION##PY_MINOR_VERSION
#define _BOOST_PYTHON_CONCAT(N, M, m) N ## M ## m
#define BOOST_PYTHON_CONCAT(N, M, m) _BOOST_PYTHON_CONCAT(N, M, m)
#define BOOST_LIB_NAME BOOST_PYTHON_CONCAT(boost_numpy, PY_MAJOR_VERSION, PY_MINOR_VERSION)
//
// If we're importing code from a dll, then tell auto_link.hpp about it:
//
#ifdef BOOST_NUMPY_DYNAMIC_LIB
# define BOOST_DYN_LINK
#endif
//
// And include the header that does the work:
//
#include <boost/config/auto_link.hpp>
#endif // auto-linking disabled#undef BOOST_PYTHON_CONCAT
#undef _BOOST_PYTHON_CONCAT#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
#endif // CONFIG_NUMPY20170215_H_
文件C:\Boost\include\boost-1_67\boost\python\detail\config.hpp
#define _BOOST_PYTHON_CONCAT(N, M, m) N ## M ## m
#define BOOST_PYTHON_CONCAT(N, M, m) _BOOST_PYTHON_CONCAT(N, M, m)
#define BOOST_LIB_NAME BOOST_PYTHON_CONCAT(boost_python, PY_MAJOR_VERSION, PY_MINOR_VERSION)
//
// If we're importing code from a dll, then tell auto_link.hpp about it:
//
#ifdef BOOST_PYTHON_DYNAMIC_LIB
# define BOOST_DYN_LINK
#endif
//
// And include the header that does the work:
//
#include <boost/config/auto_link.hpp>
#endif // auto-linking disabled
#undef BOOST_PYTHON_CONCAT
#undef _BOOST_PYTHON_CONCAT
#ifndef BOOST_PYTHON_NO_PY_SIGNATURES
#define BOOST_PYTHON_SUPPORTS_PY_SIGNATURES // enables smooth transition
#endif
#if !defined(BOOST_ATTRIBUTE_UNUSED) && defined(__GNUC__) && (__GNUC__ >= 4)
# define BOOST_ATTRIBUTE_UNUSED __attribute__((unused))
#endif
二、在VS2010中引用
在 Project→Project Property→Configuration Properties→VC++ Directories 中,Library Directories 中需要包含C:\Boost\lib;C:\ProgramData\Anaconda3\libs;$(LibraryPath);Include Directories 中需要包含C:\Boost\include\boost-1_67;C:\ProgramData\Anaconda3\include;$(IncludePath)
另外要选择MD多线程。
三、在C++中如何调用python3的脚本
样例代码如下:
class ECT_PROCESSOR
{
public:
ECT_PROCESSOR()
{
Param1="";
Param2="";
Param3="";
Param4="";
Param5="";
Param6="";
Param7="";
Param8="";
Param9="";
Param10="";
dimx=0;
dimy=0;
dimz=0; }
~ECT_PROCESSOR()
{ }
void SetModel(Model * in_p_model)
{
p_model=in_p_model;
} bool SetSrcValue(int x, int y,int z,int tag, short val)
{ }
short GetSrcValue(int x, int y,int z,int tag)
{ } bool SetMaskValue(int x, int y,int z, unsigned char val)
{ }
unsigned char GetMaskValue(int x, int y,int z)
{ }
std::string Param1;
std::string Param2;
std::string Param3;
std::string Param4;
std::string Param5;
std::string Param6;
std::string Param7;
std::string Param8;
std::string Param9;
std::string Param10;
int dimx;
int dimy;
int dimz; private:
Model *p_model;
}; ECT_PROCESSOR* GetECT_PROCESSORInstance()
{
static ECT_PROCESSOR* the_ECT_PROCESSOR = NULL;
if (!the_ECT_PROCESSOR)
{
the_ECT_PROCESSOR = new ECT_PROCESSOR();
the_ECT_PROCESSOR->SetModel(Model::GetModelInstance());
}
return the_ECT_PROCESSOR;
} // export c++ function and class to python
BOOST_PYTHON_MODULE(MyEngine)
{
using namespace boost::python;
def("GetECT_PROCESSORInstance", GetECT_PROCESSORInstance,
return_value_policy< reference_existing_object >());
class_<ECT_PROCESSOR>("ECT_PROCESSOR", "ECT_PROCESSOR")
.def("SetSrcValue", &ECT_PROCESSOR::SetSrcValue,
args("x", "y","z","tag""val"))
.def("GetSrcValue", &ECT_PROCESSOR::GetSrcValue,
args("x","y","z","tag"))
.def("SetMaskValue", &ECT_PROCESSOR::SetMaskValue,
args("x","y","z","val"))
.def("GetMaskValue", &ECT_PROCESSOR::GetMaskValue,
args("x", "y","z"))
.def_readonly("Param1", &ECT_PROCESSOR::Param1,"Param1")
.def_readonly("Param2", &ECT_PROCESSOR::Param2,"Param2")
.def_readonly("Param3", &ECT_PROCESSOR::Param3,"Param3")
.def_readonly("Param4", &ECT_PROCESSOR::Param4,"Param4")
.def_readonly("Param5", &ECT_PROCESSOR::Param5,"Param5")
.def_readonly("Param6", &ECT_PROCESSOR::Param6,"Param6")
.def_readonly("Param7", &ECT_PROCESSOR::Param7,"Param7")
.def_readonly("Param8", &ECT_PROCESSOR::Param8,"Param8")
.def_readonly("Param9", &ECT_PROCESSOR::Param9,"Param9")
.def_readonly("Param10", &ECT_PROCESSOR::Param10,"Param10")
.def_readonly("dimx", &ECT_PROCESSOR::dimx,"dimx")
.def_readonly("dimy", &ECT_PROCESSOR::dimy,"dimy")
.def_readonly("dimz", &ECT_PROCESSOR::dimz,"dimz")
;
}
bool InitPython()
{
Py_Initialize(); if(!Py_IsInitialized())
{
return false;
}
return true;
}
int Controller::ExcutePythonScript_ECT(CString sInFilePath,CString sInFileName,CString sInParam1,CString sInParam2,CString sInParam3,CString sInParam4,CString sInParam5,
CString sInParam6,CString sInParam7,CString sInParam8,CString sInParam9,CString sInParam10,CString *pOutMsg)
{
int tmp_ectindex=model->GetActiveEctIndex();
if(tmp_ectindex<0)
{
//报错
OutputLogB("Log.log", __FILE__, __LINE__, "model->GetActiveEctIndex<0");
return -1;
}
try
{
CString sModuleName;
std::string stdModuleName;
std::string stdModulePath(sInFilePath.GetBuffer());
int pos = sInFileName.ReverseFind('.');
if ( pos > 0 )
{
sModuleName = sInFileName.Left(pos);
stdModuleName=sModuleName.GetBuffer(0);
}
else
{
stdModuleName=sInFileName.GetBuffer(0);
} std::ifstream fin;
char sFullPath[256]={0};
snprintf(sFullPath,sizeof(sFullPath),"%s\\%s",stdModulePath.c_str(),sInFileName.GetBuffer());
OutputLogB("Log.log", __FILE__, __LINE__, "fin.open(%s)",sFullPath);
fin.open(sFullPath);
std::string str;
std::string str_in = "";
while (getline(fin, str)) //一行一行地读到字符串str_in中
{
str_in = str_in + str + '\n';
}
fin.close(); using namespace boost::python; if (PyImport_AppendInittab(const_cast<char*>("MyEngine"),
#if PY_VERSION_HEX >= 0x03000000
PyInit_MyEngine
#else
initMyEngine
#endif
) == -1)
{
OutputLogB("Log.log", __FILE__, __LINE__,"Failed to add embedded_hello to the interpreter's builtin modules");
return -2;
} //PyImport_AppendInittab( stdModuleName.c_str(), &PyInit_MyEngine );
if(!InitPython())
{
return -1;
}
PyInit_MyEngine(); // init MyEngine Module // Add current path to sys.path. You have to
// do this in linux. While in Windows,
// current path is already in sys.path. ECT_PROCESSOR* pECT_PROCESSOR = GetECT_PROCESSORInstance();
pECT_PROCESSOR->Param1=sInParam1.GetBuffer();
pECT_PROCESSOR->Param2=sInParam2.GetBuffer();
pECT_PROCESSOR->Param3=sInParam3.GetBuffer();
pECT_PROCESSOR->Param4=sInParam4.GetBuffer();
pECT_PROCESSOR->Param5=sInParam5.GetBuffer();
pECT_PROCESSOR->Param6=sInParam6.GetBuffer();
pECT_PROCESSOR->Param7=sInParam7.GetBuffer();
pECT_PROCESSOR->Param8=sInParam8.GetBuffer();
pECT_PROCESSOR->Param9=sInParam9.GetBuffer();
pECT_PROCESSOR->Param10=sInParam10.GetBuffer(); object main_module = import( "__main__" );
object main_namespace = main_module.attr( "__dict__" ); object ignored = exec(
"import sys\n"
"sys.path.append('.')\n", main_namespace ); int dimxyz[3]={0};
model->GetActiveMask()->GetDimensions(dimxyz);
pECT_PROCESSOR->dimx=dimxyz[0];
pECT_PROCESSOR->dimy=dimxyz[0];
pECT_PROCESSOR->dimz=dimxyz[0]; object ignored2 = exec(str_in.c_str(),
main_namespace); return 0; }
catch (boost::python::error_already_set const &)
{
std::string perror_str = parse_python_exception();
pOutMsg->SetString(perror_str.c_str());
PyErr_Print();
PyErr_Clear();
//delete _module;
//_module = NULL;
// Py_Finalize();
return -2;
} }
C++ boost.python折腾笔记的更多相关文章
- Boost Python学习笔记(四)
你将学到什么 在Python中调用C++代码时的传参问题 基础类型 Python的字符串是常量,所以C++函数参数中的std::string &必须为const 修改源文件(main.cpp) ...
- Boost Python学习笔记(五)
你将学到什么 在C++中调用Python代码时的返回值问题 基础类型 修改Python脚本(build/zoo.py) def rint(): return 2 def rstr(): return ...
- Boost Python学习笔记(二)
你将学到什么 如何在Python中调用C++代码 如何在C++中调用Python代码 在Python中调用C++代码 首先定义一个动物类(include/animal.h) #pragma once ...
- Boost Python学习笔记(三)
你将学到什么 在C++中调用Python代码时的传参问题 基础类型 继续使用前面的项目,但是先修改下Python脚本(zoo.py),添加Add和Str函数,分别针对整数.浮点数和字符串参数的测试 d ...
- Boost Python学习笔记(一)
开发环境搭建 下载源码 boost_1_66_0.tar.gz 生成编译工具 # tar axf boost_1_66_0.tar.gz # cd boost_1_66_0 # yum install ...
- boost.python笔记
boost.python笔记 标签: boost.python,python, C++ 简介 Boost.python是什么? 它是boost库的一部分,随boost一起安装,用来实现C++和Pyth ...
- python学习笔记:安装boost python库以及使用boost.python库封装
学习是一个累积的过程.在这个过程中,我们不仅要学习新的知识,还需要将以前学到的知识进行回顾总结. 前面讲述了Python使用ctypes直接调用动态库和使用Python的C语言API封装C函数, C+ ...
- python学习笔记--Django入门0 安装dangjo
经过这几天的折腾,经历了Django的各种报错,翻译的内容虽然不错,但是与实际的版本有差别,会出现各种奇葩的错误.现在终于找到了解决方法:查看英文原版内容:http://djangobook.com/ ...
- Python学习笔记(一)类和继承的使用
一年前就打算学Python了,折腾来折腾去也一直没有用熟练,主要是类那一块不熟,昨天用Python写了几个网络编程的示例,感觉一下子迈进了很多.这几天把学习Python的笔记整理一下,内容尽量简洁. ...
随机推荐
- C# 乐观锁、悲观锁、共享锁、排它锁、互斥锁
悲观锁(Pessimistic Lock), 顾名思义,就是很悲观,每次去拿数据的时候都认为别人会修改,所以每次在拿数据的时候都会上锁,这样别人想拿这个数据就会block直到它拿到锁.传统的关系型数据 ...
- Linux源码安装JDK1.8
Linux源码安装Java 1.到官网下载 jdk-8u131-linux-x64.tar.gz 官网地址:http://www.oracle.com/technetwork/java/javase/ ...
- Redhat系统上开启Telnet服务
https://blog.csdn.net/wolfofsiberian/article/details/51635952 1.操作系统 Redhat Step1:修改配置文件/etc/xinetd. ...
- 跳转Activity时清除当前Activity
void GotoMainActivity(){ Intent intent = new Intent(ProductionInformationActivity.this, MainActivity ...
- oracle数据入库
oracle数据入库 注意:先要处理文件中的分隔符 将数据分列 创建为标准的sql语句 1.在oracle数据库中创建要入库的表如果有该表则不用创建(注:创建字段的数据类型要符合实际逻辑 va ...
- CentOS7下开放端口
memcached等服务启动后,外网默认是无法访问的,因为防火墙不允许,所以要开启防火墙,让其可以访问这些端口号. 方法一:使用firewall 1.运行命令:firewall-cmd --get-a ...
- Redis分布式缓存
Redis 主 slave 数据库优化-- 加inex, 分区 JVM调优--参数设置,比如偏向于计算的如何设置? 线程池:queue放满了之后,有什么方式能让他不拒绝掉?blockqueue就等在那 ...
- 20175234 2018-2019-2 《Java程序设计》第六周学习总结
20175234 2018-2019-2 <Java程序设计>第六周学习总结 教材学习内容总结 第七章内部类和异常类 总结 Java支持在一个类中声明另一个类,这样的类称作内部类,而包含内 ...
- 使用Tenorshare iCareFone for mac为iPhone做系统修复
tenorshare icarefonemac中文版采用一键式方法来保护,修理,清洁,优化并最终加快您的iPhone,iPad和iPod的速度.它可以帮助您轻松解决所有iOS问题,并让您的iPhone ...
- 服务管理之openssh
1. 使用 SSH 访问远程命令行 1.1 OpenSSH 简介 OpenSSH这一术语指系统中使用的Secure Shell软件的软件实施.用于在远程系统上安全运行shell.如果您在可提供ssh服 ...