In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article is about how to understand the specific implementation of Python binding C++ program, the editor feels very practical, so share it with you to learn, I hope you can get something after reading this article, say no more, follow the editor to have a look.
Python programming language has a wide range of applications and flexible application methods, which can easily help developers to achieve some specific functional requirements. For example, today to introduce for you about the Python binding C++ program related operations, you can understand the application characteristics of this language.
There are times when you need to provide C++ programs with flexibility, and scripting languages become very important here. Using Boost.Python to add a layer of shell to the C++ program is relatively simple and concise, and there is no intrusiveness to the original C++ code. I tried it today, and it feels good. I can integrate it into the project I'm working on right now.
The procedure of binding a C++ program for Python is basically as follows:
(1) write a Boost.Python wrapper for C++ class
(2) compile to so
(3) you can call in python.
For the example of David Abrahams, my source file is as follows:
Python binds C++ program example 1:hello world function
(1) hello.cpp
# include
< stdexcept>Char const* greet (unsigned x) {static char const* const msgs [] = {"hello", "Boost.Python", "world!"}; if (x > 2) throw std::range_error ("greet: index out of range"); return msgs [x];}
(2) hello_wrap.cpp
# include
< boost/python.hpp>Using namespace boost::python; char const* greet (unsigned x); BOOST_PYTHON_MODULE (hello) {def ("greet", greet, "return one of 3 parts of a greeting");}
(3) makefile
PYTHON_INCLUDE_FLAGS =\-I/usr/include/python2.4 LIB_FLAGS =\-lboost_python SOURCE =\ hello.cpp hello_wrap.cpp all:$ {SOURCE} hello.so clean + ${PYTHON_INCLUDE_FLAGS} ${SOURCE} ${LIB_FLAGS}-shared-o hello.so clean: rm-f hello * .o * .out * .so
(4) hello.py
Import hello for x in range (3): print hello.greet (x)
Python binds C++ program example 2:hello world class
(1) hello_class.cpp
# include
< boost/python.hpp># include
< iostream>Using namespace std; using namespace boost::python; class World {public: void set (std::string msg) {this- > msgmsg = msg;} void greet () {cout
< < this->Msg
< < endl; } string msg; }; BOOST_PYTHON_MODULE(hello) { class_< World>W ("World"); w.def ("greet", & World::greet); w.def ("set", & World::set);}
(2) makefile
PYTHON_INCLUDE_FLAGS =\
-I/usr/include/python2.4
LIB_FLAGS =\
-lboost_python
SOURCE =\
Hello_class.cpp
All:$ {SOURCE}
Gmail + ${PYTHON_INCLUDE_FLAGS} ${SOURCE} ${LIB_FLAGS}
-shared-o hello.so
Clean:
Rm-f hello * .o * .out * .so (3) hello_class.py
Import hello
Planet = hello.World ()
Planet.set ('howdy')
Planet.greet ()
The above is how to understand the specific implementation of Python binding C++ program, the editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.