python和其它语言混合编程_C++和Python混合编程 - python嵌入C++

早就听说过了对于复杂的系统一般会结合多种语言进行开发,最近摸索了下C++和Python混合编程,在此总结一下,做为笔记。

对于C++和python混合编程实际上包含两部分内容:

用C++写python扩展(extending): 即所谓的在python中调用C/C++代码,一般用于对效率要求高核心模块用C/C++编写,通过一些处理后生成动态库如cppmod.so,然在python代码中import cppmod,即可调用C/C++

python脚本嵌入到C++程序中(embedding): 即所谓的C++代码调用python,C++ app要嵌入python解析器从而调用python代码

就实现而言,对于这两者都可以直接用Python/C API,具体可以去参考python文档,但都比较麻烦

对于1, 主要的方案有 boost.python 以及swig, 后者不太了解,本篇主要讲利用boost.python,对C/C++处理后生成动态库,python导入后调用C/C++代码

对于2, C/C++代码中嵌入python,也可以用 boost.python,当然直接 Python/C API也可以

##一、Python C API写 python embedding

python嵌入 C/C++ 的程序,我们需要在一条 #include 语句处含入 Python.h,并在程序开始时调用 Py_Initialize(),在程序结束时调用 Py_Finalize()。

#include

// using python c API to wrte a python embedding

const static char* python_code = "\

from time import time,ctime\n\

print 'Today is', ctime(time())\n";

int main()

{

Py_Initialize();

PyRun_SimpleString(python_code);

Py_Finalize();

return 0;

}

编译:

[tanli@p04bc boostpython]$g++ sampleEmbed.cpp -I /usr/include/python2.7 -lpython2.7 -o sampleEmbed

[tanli@p04bc boostpython]$./sampleEmbed

Today is Fri Nov 22 10:18:50 2013

##二、boost.python 写 python embedding

boost.python 写python embedding更简单些,exec执行一个语句,exec_file执行一个python程序

#include

#include

using namespace std;

using namespace boost::python;

int main(int argc, char *argv[])

{

Py_Initialize();

object main_module = import("__main__");

object main_namespace = main_module.attr("__dict__");

exec("hello = file('hello.txt', 'w')\n"

"hello.write('Hello world!')\n"

"hello.close()",

main_namespace);

exec("result = 5 ** 2", main_namespace);

int five_squared = extract(main_namespace["result"]);

cout << "The five_squeared caculated by python is " << five_squared << endl;

// Load the sys module.

object sys = import("sys");

// Extract the python version.

std::string version = extract(sys.attr("version"));

std::cout << version << std::endl;

object simple = exec_file("simple.py",main_namespace, main_namespace);

object add = main_namespace["add"];

int val = extract(add(5, 100));

cout << "Python has caculated add as " << val << endl;

return 0;

}

编译运行下:

[tanli@p04bc boostpython]$./embeddByBooostPy

The five_squeared caculated by python is 25

2.7 (r27:82500, Oct 20 2010, 13:31:35)

[GCC 3.4.5 20051201 (Red Hat 3.4.5-2)]

Python has caculated add as 105

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值