自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Python綁定C++程序具體實現(xiàn)方法淺談

開發(fā) 后端
Python綁定C++程序在實際應用中是一個比較重要的操作技術。對于大多數(shù)開發(fā)人員來說,掌握這一應用技巧是非常必要的一個技能。

Python編程語言的應用范圍比較廣泛,應用方式靈活,可以很方便的幫助開發(fā)人員實現(xiàn)一些特定的功能需求。比如今天為大家介紹的有關Python綁定C++程序的相關操作,大家就可以從中了解到這一語言的應用特點。#t#

很多時候需要給C++程序提供一種使用上的靈活性,腳本語言在這里就變得很重要了。采用Boost.Python為C++程序加一層shell,比較簡單、簡潔,對原有的C++代碼也沒有侵入性。今天試了一下,感覺不錯,可以把它集成在現(xiàn)在正在做的項目中。

為Python綁定C++程序過程基本上如下:

(1)為C++類編寫一個Boost.Python wrapper

(2)編譯成so

(3)可以在python中調用了

針對David Abrahams的例子,偶的源文件如下:

Python綁定C++程序例1:hello world 函數(shù)

(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++實現(xiàn)程序

2010-02-03 09:59:42

C++文件流操作

2010-02-01 17:02:53

C++產生隨機數(shù)

2010-01-22 13:59:34

Visual C++應

2010-02-02 18:01:47

C++字符串替換函數(shù)

2010-02-06 11:19:33

C++獲取文件

2010-02-03 16:35:45

C++回文

2010-02-03 10:50:33

C++多態(tài)

2010-02-02 16:23:46

C++實現(xiàn)WPF動畫

2024-02-26 07:26:27

RustC++開發(fā)

2010-01-26 09:50:30

C++接口

2010-01-18 14:41:52

Visual C++開

2010-02-02 17:13:35

C++ Endian

2011-07-20 17:23:29

C++持久對象

2010-02-03 13:26:53

C++計時

2009-12-03 15:45:51

PHP加入數(shù)據程序

2010-02-01 13:34:07

C++獲得系統(tǒng)時間

2010-02-06 10:09:47

C++模擬event關
點贊
收藏

51CTO技術棧公眾號