Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1239626
  • 博文数量: 76
  • 博客积分: 1959
  • 博客等级: 上尉
  • 技术积分: 2689
  • 用 户 组: 普通用户
  • 注册时间: 2007-11-19 12:07
个人简介

樽中酒不空

文章分类

全部博文(76)

文章存档

2020年(4)

2019年(1)

2017年(2)

2016年(2)

2015年(7)

2014年(11)

2013年(13)

2012年(18)

2011年(2)

2010年(16)

分类: C/C++

2013-08-05 16:09:08


一 编译安装
1 下载安装Python2.7 32位版本,安装
2 下载boost .7z压缩包 ,按32位方式编译
(如果Python是64位的话,经常出现Py_NoStruct链接错误。)

二 测试嵌入程序:
很多sample只是简单使用c++调用python的函数或类执行某个动作,对于返回值说明的比较少。这里主要讲解返回多个值的处理。
1 新建console工程
2 代码如下:
#include
using namespace boost::python;
using namespace std;


void exec_test()
{
object main = import("__main__");
object global(main.attr("__dict__"));
object result = exec(
"def greet():               \n"
"    var='a,b,c,d'.split(',') \n"
"    return len(var),var \n", //这里返回两个值,一个是数量,另一个是字符串链表
global, global);
 
  object greet = global["greet"];
   object r = greet(); //执行函数,返回的是数组

int ll = extract(r[0]); //第一个元素表示数组长度
for (int i = 0; i < ll; i++)
{
string message = extract(r[1][i]);
cout << message << endl;
}
}


int _tmain(int argc, _TCHAR* argv[])
{

    Py_Initialize ();//初始化python环境  
    if(!Py_IsInitialized())  
    {  
        cout<<"------python初始化失败"<
        return 0;  
    }  
  
    try  
    {  
     
 exec_test();
    }  
    catch(...)  
    {  
        if (PyErr_Occurred())  
            PyErr_Print();  
    }  
  
return 0;
 

阅读(3253) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~