打通Python和C++之后?你懂的!

开发 后端
Python作为世界上最好的 胶水 语言(哼,世界上最好的语言当然是PHP==),利用Python的简洁和C++的高效,基本可以解决99%的问题了吧~

Python作为世界上***的 胶水 语言(哼,世界上***的语言当然是PHP==),利用Python的简洁和C++的高效,基本可以解决99%的问题了吧~

一般的,Python和C++的交互分为这两种情况:

  1. 用C++扩展Python:当一个Python项目中出现了性能瓶颈时,将瓶颈部分抽离出来,用C++封装成一个Python可以调用的模块(so库);
  2. 将Python内嵌入C++:当一个C++项目中有部分功能预期将会经常变更需求,期望获得更高的灵活性时,将这部分功能用Python实现,在C++中进行调用。这篇文章将简单介绍下***部分的一种做法。

打通Python和C++

Boost.Python

Boost作为一个大宝库,提供了我们所需要的这一功能。并且,在Boost的许多库中,已经默认使用了Boost.Python,所以也算是经过了充分的测试。

安装

Boost的大部分功能都是以头文件的形式提供的,无需安装;但是也有少部分功能,需要进行手动编译。不幸,Boost.Python也是其中之一。

参照 Getting Started on Unix Variants 的第五部分内容,即可安装Boost.Python。安装完成后,可以在相关目录(我的是/usr/local/lib下)看到相关的so文件。

Hello World

用C++实现一个模块,在Python中调用时,可以返回一个特定的字符串。

++

 

  1. #include <boost/python.hpp> 
  2.  
  3. char const* greet() 
  4.     return "hello, boost"
  5.  
  6. BOOST_PYTHON_MODULE(hello_boostpy) 
  7.     using namespace boost::python; 
  8.     def("greet", greet); 

太简单了,代码基本说明了一切~

将其编译成动态链接库的形式:

  1. g++ -I /usr/include/python2.7/ -fPIC -shared -o hello_boostpy.so hello_boostpy.cc -lboost_python 

这时可以使用ldd看看hello_boostpy.so可不可以找到libboost_python,找不到的话,需要手动将其路径加入环境变量LD_LIBRARY_PATH中,或者用ldconfig相关的命令也可以。

接下来就可以在Python中使用hello_boostpy库了:

 

  1. # -*- coding: utf-8 -*- 
  2. import sys 
  3. sys.path.append('.'
  4.  
  5.  
  6. def test(): 
  7.     import hello_boostpy 
  8.     return hello_boostpy.greet() 
  9.  
  10.  
  11. if __name__ == "__main__"
  12.     print test() 

Expose Class

接下来,我们在C++实现的模块中,添加一个类,并且尝试向C++方向传入Python的list类型对象。

C++类:

++

 

  1. #include <boost/python.hpp> 
  2. #include <vector> 
  3. #include <string> 
  4. #include <sstream> 
  5. using namespace boost::python; 
  6.  
  7. struct Person 
  8.     void set_name(std::string name) { this->name = name; } 
  9.     std::string print_info(); 
  10.     void set_items(list& prices, list& discounts); 
  11.      
  12.      
  13.     std::string name
  14.     std::vector<double> item_prices; 
  15.     std::vector<double> item_discounts; 
  16. }; 

其中,Python方的list类型,在Boost.Python中有一个对应的实现boost::python::list(相应的,dict、tuple等类型都有对应实现)。在set_items中,我们将会用boost::python::extract对数据类型做一个转换。

++

 

  1. void Person::set_items(list& prices, list& discounts) 
  2.     for(int i = 0; i < len(prices); ++i) 
  3.     { 
  4.         double price = extract<double>(prices[i]); 
  5.         double discount = extract<double>(discounts[i]); 
  6.         item_prices.push_back(price); 
  7.         item_discounts.push_back(discount); 
  8.     } 

Python模块定义部分依旧是非常直观的代码:

 

  1. BOOST_PYTHON_MODULE(person) 
  2.     class_<Person>("Person"
  3.         .def("set_name", &Person::set_name) 
  4.         .def("print_info", &Person::print_info) 
  5.         .def("set_items", &Person::set_items) 
  6.     ;    

在Python代码中,就可以像使用一个Python定义的类一样使用Person类了:

 

  1. # -*- coding: utf-8 -*- 
  2. import sys 
  3. sys.path.append('.'
  4.  
  5.  
  6. def test(): 
  7.     import person 
  8.     p = person.Person() 
  9.     p.set_name('Qie'
  10.     p.set_items([100, 123.456, 888.8], [0.3, 0.1, 0.5]) 
  11.     print p.print_info() 
  12.  
  13.  
  14. if __name__ == "__main__"
  15.     test() 

Py++

上面的模块封装过程,看上去还是有些枯燥,有不少地方都是重复的工作。那么可不可以自动的进行呢?Py++提供了这样的能力,它可以帮你自动生成Boost.Python的相关代码,对于接口数量比较多的模块来说,可以极大的减少工作量,也减少了出错的概率。具体使用方法,可以参见 Tutorial

责任编辑:未丽燕 来源: 一根笨茄子
相关推荐

2023-07-17 10:28:00

C/C++编程接口

2009-10-22 09:17:16

C++ CLR

2021-07-23 16:30:36

PythonC++代码

2021-03-26 10:35:49

C++Python编程语言

2019-01-21 09:02:03

C++Python编程语言

2011-04-11 09:43:25

C++C

2018-05-15 11:14:07

面试官C++编程

2014-01-24 09:49:01

C++指针

2024-02-26 07:26:27

RustC++开发

2022-07-01 11:56:54

C语言C++编程语言

2010-01-28 15:22:12

C++嵌套类

2011-05-18 18:05:47

C#C++

2009-09-16 14:56:23

C++

2021-02-26 10:41:59

C++程序员代码

2011-05-18 17:56:38

C#C++

2009-09-04 17:34:11

C#CC++

2011-04-06 08:57:07

C++java多态

2020-06-17 12:22:44

C覆盖重载

2023-03-15 15:58:11

Python动态库C++

2009-08-19 10:09:21

C#和C++
点赞
收藏

51CTO技术栈公众号