使用boost实现c++与python的相互调用

学习boost.python 模块,我的环境windows上qt5.5 mingw4.92, 设置好环境变量。
1.环境搭建:在python已安装的包里面有一个 include文件夹 里面存放的是需要的头文件, libs文件夹 里面存放的是需要的库文件, 若想调用这里面的库 可以用ide工具把头文件和库文件路径都指定一下, 这样就可以被 boost.python 调用 了

2.生成动态库:我是用qt creator来创建的 , 最终生成的是dll文件 我发现dll文件无法直接被import 于是尝试把dll文件 改成.pyd后缀的文件, 就可以执行了
生成的库文件

修改之后存放到python的目录下面

执行python hello.py



cpu直接超过80%了, python调用c++的库可以充分使用多核


附上代码:

c++调用python的方法

#include <iostream>
#include <python27/Python.h>
#include <boost/python.hpp>
#include <iostream>
#include <string>
#include <string.h>
#include <fstream>

using namespace std;
using namespace boost::python;

int main() {
    //初始化读取 py 文件的信息
    std::ifstream fin;
    fin.open("D:/py_test/test.py");
    std::string str;
    std::string str_in = "";
    while (getline(fin, str))    //一行一行地读到字符串str_in中
    {
        str_in = str_in + str + '\n';
    }
    fin.close();
    cout<<str_in<<endl;
    Py_Initialize();
//    PyRun_SimpleString("from time import time,ctime\n"
//    "print 'Today is',ctime(time())\n");
    PyRun_SimpleString(str_in.c_str());
    Py_Finalize();
    cout<<"it is over ...."<<endl;
    return 0;
}



c++扩展python

#include <boost/python/module.hpp>
#include <boost/python/def.hpp>
#include <iostream>
//头文件是我自己设置的位置
#include <python27/Python.h>
#include <boost/python.hpp>
#include <boost/thread/thread.hpp>
#include <boost/bind.hpp>
#include <boost/thread/mutex.hpp>
using namespace std;
using namespace boost;
using namespace boost::python;


void test(){
    string sum;
    while(1)
    {
        string ss = "111111112222222";
        sum = sum + ss;
    }
}

char const* greet()
{
    thread_group group;
    for(int num=0;num<10;num++)
        group.create_thread(bind(test));
    group.join_all();
   return "hello, world";
}

BOOST_PYTHON_MODULE(hello_world)
{
    using namespace boost::python;
    def("greet", greet);
}



python测试代码

#! /usr/bin/env python
#  Copyright Joel de Guzman 2002-2007. Distributed under the Boost
#  Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt
#  or copy at http://www.boost.org/LICENSE_1_0.txt)
#  Hello World Example from the tutorial

import hello_world
print(hello_world.greet())



  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
为了方便大家使用MinGW(GCC)+_boost.python,特意只做了三个dll,可以很方便地将c++代码转为python模块. libboost_python-mgw45-1_49.dll libboost_python-mgw45-d-1_49.dll python27.dll 这三个文件我已放在资源里面,大家可以下载. 下面说说使用方法: 第一步:编写一个hello_ext.cpp的c++源文件 #include <boost/python.hpp> // 第一行必须是#include <boost/python.hpp> // 否则会留下一点小问题 #include <vector> // 输出字符串 char const* greet() { return "hello, world"; } // 实现两个数字相加 int add(int x, int y) { return x + y; } // 打印vector的函数 void vprint() { std::vector<int>myvector; for (int i = 1; i <= 5; i++) { myvector.push_back(i); } std::vector<int>::iterator it; std::cout << "myvector contains:"; for (it = myvector.begin(); it < myvector.end(); it++) { std::cout << " " << *it; } std::cout << std::endl; } // 定义python模块的接口文件 BOOST_PYTHON_MODULE(hello_ext) { // hello_ext为导出python模块的名字 using namespace boost::python; def("greet", greet); // 导出函数greet def("add", add); // 导出函数add def("vprint", vprint); // 导出函数vprint } 将上面的文件一定要保存为utf-8的格式(使用记事本在保存的时候就可以选择),不推荐Ansi格式! 然后就可以使用下面的命令编译python模块了: g++ -o hello_ext.pyd hello_ext.cpp -shared libboost_python-mgw45-1_49.dll python27.dll 也可以使用如下的命令,编译debug版本 g++ -o hello_ext.pyd hello_ext.cpp -shared libboost_python-mgw45-d-1_49.dll python27.dll 运行上面的命令之前,请确保hello_ext.cpp,libboost_python-mgw45-1_49.dll,libboost_python-mgw45-d-1_49.dll和 python27.dll在同一个目录. hello_ext.pyd就是python中能直接使用的动态链接库,windows一般以dll为后缀,而python只承认pyd文件. 下面来测试一下: import hello_ext print hello_ext.greet() print hello_ext.add(1,3) hello_ext.vprint() 输出为: hello, world 4 myvector contains: 1 2 3 4 5 看,成功了! ============================================================================= 使用g++编译常见的问题就是找不到文件<boost/python.hpp>和pyconfig.h等文件. 这些文件其实在boost的目录下面和C:\Python27\include目录中. 为了使用方便,将整个\boost_1_49_0\boost\目录复制到MinGw的include目录下面; 将C:\Python27\include目录下的文件全部复制到MinGw的include目录下面即可. 如果不想复制,也可以给g++设置-L参数 -LC:\boost\include\boost_1_49_0\ 和-LC:\Python27\include, 不过每次都这样,还是麻烦,不如复制一下彻底解决! 在发布hello_ext.pyd的时候,由于是动态链接库,所以不要忘了libboost_python-mgw45-1_49.dll, libboost_python-mgw45-d-1_49.dll和 python27.dll也要一起发布!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值