为了让当年研究生时写的图像处理系统重出江湖起到更大的作用,应研究生导师的意见,对原有的c++框架做了python扩展处理,为了避免遗忘,备注如下:

一、boost 编译

下载boost源码,这里使用boost 1.67,解压到目录,进行编译

  • 下载C++的boost库:http://www.boost.org/
  • 安装Anaconda3-5.1.0-Windows-x86_64 默认路径安装
  • 解压boost文件,在其目录中执行.\bootstrap.bat,会生成编译器b2.exebjam.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中引用

ProjectProject PropertyConfiguration PropertiesVC++ 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折腾笔记的更多相关文章

  1. Boost Python学习笔记(四)

    你将学到什么 在Python中调用C++代码时的传参问题 基础类型 Python的字符串是常量,所以C++函数参数中的std::string &必须为const 修改源文件(main.cpp) ...

  2. Boost Python学习笔记(五)

    你将学到什么 在C++中调用Python代码时的返回值问题 基础类型 修改Python脚本(build/zoo.py) def rint(): return 2 def rstr(): return ...

  3. Boost Python学习笔记(二)

    你将学到什么 如何在Python中调用C++代码 如何在C++中调用Python代码 在Python中调用C++代码 首先定义一个动物类(include/animal.h) #pragma once ...

  4. Boost Python学习笔记(三)

    你将学到什么 在C++中调用Python代码时的传参问题 基础类型 继续使用前面的项目,但是先修改下Python脚本(zoo.py),添加Add和Str函数,分别针对整数.浮点数和字符串参数的测试 d ...

  5. Boost Python学习笔记(一)

    开发环境搭建 下载源码 boost_1_66_0.tar.gz 生成编译工具 # tar axf boost_1_66_0.tar.gz # cd boost_1_66_0 # yum install ...

  6. boost.python笔记

    boost.python笔记 标签: boost.python,python, C++ 简介 Boost.python是什么? 它是boost库的一部分,随boost一起安装,用来实现C++和Pyth ...

  7. python学习笔记:安装boost python库以及使用boost.python库封装

    学习是一个累积的过程.在这个过程中,我们不仅要学习新的知识,还需要将以前学到的知识进行回顾总结. 前面讲述了Python使用ctypes直接调用动态库和使用Python的C语言API封装C函数, C+ ...

  8. python学习笔记--Django入门0 安装dangjo

    经过这几天的折腾,经历了Django的各种报错,翻译的内容虽然不错,但是与实际的版本有差别,会出现各种奇葩的错误.现在终于找到了解决方法:查看英文原版内容:http://djangobook.com/ ...

  9. Python学习笔记(一)类和继承的使用

    一年前就打算学Python了,折腾来折腾去也一直没有用熟练,主要是类那一块不熟,昨天用Python写了几个网络编程的示例,感觉一下子迈进了很多.这几天把学习Python的笔记整理一下,内容尽量简洁. ...

随机推荐

  1. usb设备(移动硬盘或U盘),弹出时提示“有进程或程序占用,无法弹出”。解决办法

    测试环境:Win7(其他Windows系统环境,也可参考) 总结办法来源,https://bbs.csdn.net/topics/392297251?page=1文章中热心网友的评论指引 1. 控制面 ...

  2. MYSQL批量删除大量数据

    DELETE FROM '表' WHERE '字段'=1 会发现删除失败,因为lock wait timeout exceed的错误: 通过LIMIT参数分批删除,因为如果不用limit,删除大量数据 ...

  3. webpack打包优化

    https://www.cnblogs.com/vvjiang/p/9327903.html

  4. 如何查看和清除svn登录信息

    切换svn登录用 点击clear,出现用户信息,勾选后点击ok 即清除 .下次访问svn需要重新登录

  5. pyautogui 文档(四):消息框功能

    消息框功能 PyAutoGUI利用PyMsgBox中的消息框函数提供跨平台的纯Python方式来显示JavaScript样式的消息框.提供了四个消息框功能: alert()函数 >>> ...

  6. python note 17 random、time、sys、os模块

    1.random模块(取随机数模块) # 取随机小数 : 数学计算 import random print(random.random())# 取0-1之间的小数 print(random.unifo ...

  7. 双硬盘,win10安装到固态盘

    1.PE下格式化固态盘的系统盘 2.打开DG分区工具,查看固态盘的系统盘是否为激活状态,红色为激活,如果不是,激活一下 3.用windows安装器,或者hdd安装win10到固态盘 4.bios中启动 ...

  8. Find out where to contain the smartforms

    Go to table E071 and give smarforms name and it will give the transport req for that. Run SE03, choo ...

  9. C# DataTable抽取Distinct数据(不重复数据)[z]

    DataTable dataTable;       DataView dataView = dataTable.DefaultView;       DataTable dataTableDisti ...

  10. linux下修改时间戳

    Linux下touch是一个非常有用的命令. touch语法结构如下: touch [-acfm][-d <日期时间>][-r <参考文件或目录>][-t <日期时间&g ...