Python绑定C++程序具体实现方法浅谈

开发 后端
Python绑定C++程序在实际应用中是一个比较重要的操作技术。对于大多数开发人员来说,掌握这一应用技巧是非常必要的一个技能。

Python编程语言的应用范围比较广泛,应用方式灵活,可以很方便的帮助开发人员实现一些特定的功能需求。比如今天为大家介绍的有关Python绑定C++程序的相关操作,大家就可以从中了解到这一语言的应用特点。#t#

很多时候需要给C++程序提供一种使用上的灵活性,脚本语言在这里就变得很重要了。采用Boost.Python为C++程序加一层shell,比较简单、简洁,对原有的C++代码也没有侵入性。今天试了一下,感觉不错,可以把它集成在现在正在做的项目中。

为Python绑定C++程序过程基本上如下:

(1)为C++类编写一个Boost.Python wrapper

(2)编译成so

(3)可以在python中调用了

针对David Abrahams的例子,偶的源文件如下:

Python绑定C++程序例1:hello world 函数

(1)hello.cpp

  1. #include < stdexcept> 
  2. char const* greet(unsigned x)  
  3. {  
  4. static char const* const msgs[] = { "hello", "Boost.Python", "world!" };  
  5. if (x > 2)   
  6. throw std::range_error("greet: index out of range");  
  7. return msgs[x];  

(2)hello_wrap.cpp

  1. #include < boost/python.hpp> 
  2. using namespace boost::python;  
  3. char const* greet(unsigned x);  
  4. BOOST_PYTHON_MODULE(hello)  
  5. {  
  6. def("greet", greet, "return one of 3 parts of a greeting");  

(3)makefile

  1. PYTHON_INCLUDE_FLAGS = \  
  2. -I/usr/include/python2.4  
  3. LIB_FLAGS = \  
  4. -lboost_python  
  5. SOURCE = \  
  6. hello.cpp hello_wrap.cpp  
  7. all:${SOURCE}  
  8. g++ ${PYTHON_INCLUDE_FLAGS} ${SOURCE} ${LIB_FLAGS} -shared -o hello.so  
  9. clean:  
  10. rm -f hello *.o *.out *.so 

(4)hello.py

  1. import hello  
  2. for x in range(3):  
  3. print hello.greet(x) 

Python绑定C++程序例2:hello world类

(1)hello_class.cpp

  1. #include < boost/python.hpp> 
  2. #include < iostream> 
  3. using namespace std;  
  4. using namespace boost::python;  
  5. class World  
  6. {  
  7. public:  
  8. void set(std::string msg) { this->msgmsg = msg; }  
  9. void greet()   
  10. {  
  11. cout < <  this->msg < <  endl;   
  12. }  
  13. string msg;  
  14. };  
  15. BOOST_PYTHON_MODULE(hello)  
  16. {  
  17. class_< World> w("World");  
  18. w.def("greet", &World::greet);  
  19. w.def("set", &World::set);  
  20. }; 

 

(2)makefile

  1. PYTHON_INCLUDE_FLAGS = \  
  2. -I/usr/include/python2.4  
  3. LIB_FLAGS = \  
  4. -lboost_python  
  5. SOURCE = \  
  6. hello_class.cpp  
  7. all:${SOURCE}  
  8. g++ ${PYTHON_INCLUDE_FLAGS} ${SOURCE} ${LIB_FLAGS} 
    -shared -o hello.so  
  9. clean:  
  10. rm -f hello *.o *.out *.so(3)hello_class.py  
  11. import hello  
  12. planet = hello.World()  
  13. planet.set('howdy')  
  14. planet.greet() 

以上就是对Python绑定C++程序的相关方法的介绍。

责任编辑:曹凯 来源: 博客园
相关推荐

2011-04-08 09:52:44

C++C#DLL

2010-02-04 11:23:25

C++反射机制

2010-01-27 15:54:49

C++实现程序

2010-02-03 09:59:42

C++文件流操作

2010-02-01 17:02:53

C++产生随机数

2010-01-22 13:59:34

Visual C++应

2010-02-02 18:01:47

C++字符串替换函数

2010-02-06 11:19:33

C++获取文件

2010-02-02 16:23:46

C++实现WPF动画

2010-02-03 10:50:33

C++多态

2010-02-03 16:35:45

C++回文

2024-02-26 07:26:27

RustC++开发

2010-02-02 17:13:35

C++ Endian

2009-12-03 15:45:51

PHP加入数据程序

2010-01-18 14:41:52

Visual C++开

2010-01-26 09:50:30

C++接口

2011-07-20 17:23:29

C++持久对象

2010-02-03 13:26:53

C++计时

2010-02-01 13:34:07

C++获得系统时间

2010-02-06 10:09:47

C++模拟event关
点赞
收藏

51CTO技术栈公众号