Written by pku_goldenlock at QQ.com introduction:
I like the speed of C ++, and I like the simplicity of Python. I believe that only C ++ can solve the 99.99% problem in Python, so how can we make them interact, here only describes howCodeInterface encapsulation allows python to call C ++. I wrote a word divider for a simple application, and I hope to display it to others in the form of web pages. I think it is more flexible and convenient to do this with Django than using C ++ XML. So how can Python code call my word divider? I'm in frontArticleIntroduction to boost. Python makes it easy to call python in C ++. Getting started with boost. Python ---- embedding python in C ++
Python embedding C ++ ------ boost. Python: how to call a function that contains variable tuple parameters and dict variables in C ++? This is called Python embedding C ++,
In fact, boost. pthon can also be used to encapsulate C ++ so that python can conveniently call C ++ code. I also wrote about how to use swig to implement C ++ encapsulation of Binary Tree display (graphical interface, console characters), simple expression evaluation, pyqt, swig first try .....
Comparison between boost. Python and swig:
I recommend that you useBoost. Python. Boost. python supports both extension and embedding of Python. For python to call C ++, both must encapsulate the required C ++ interface with the shoes code, but boost. python is more powerful, flexible, and convenient.
Why use py ++:
I am very lazy. I do not like Encapsulation by hand. It is very mechanical, as a very weakAlgorithmI don't want to spend any time writing this mechanical code, either... What if the project is very large? Many interfaces must be manually written... My God. I don't want to call it in Python anymore...
For example
Class _ <adranker> ("adranker ")
. Def ("LOAD", & adranker: load)
. Def ("getads", & adranker: getads)
. Def ("LoadFile", & adranker: LoadFile)
. Def ("getadrankinfovec", & adranker: getadrankinfovec, return_internal_reference <> ())
. Def ("getadrankinfo", & adranker: getadrankinfo, return_internal_reference <> ())
. Def ("getsegstring", & adranker: getsegstring)
. Def ("getsegstringvec", & adranker: getsegstringvec)
. Def ("getphraseorgextend", & adranker: getphraseorgextend)
. Def ("getphrasefinalextend", & adranker: getphrasefinalextend)
. Def ("getphraseextendcat", & adranker: getphraseextendcat)
;
I hope this will be automated! However, Py ++ can save the process of manual encapsulation to help you generate code similar to the above! That is, Py ++ can help you automatically generate encapsulation code manually written using boost. Python.
The following describes how to install and use Linux.
Install py ++:
PY ++ first relies on the analysis function of gccxml to parse the C ++ code structure, which is very powerful...
Wget http://www.gccxml.org/files/v0.6/gccxml-0.6.0.tar.gz
$ Mkdir gccxml-build
$ CD gccxml-build
$ Cmake...-dcmake_install_prefix: Path =/installation/path
$ Make
$ Make install
If this compilation fails, use the latest version of CVS.
CVS-D: pserver: anoncvs@www.gccxml.org:/cvsroot/gcc_xml Co gccxml
SVN Co https://pygccxml.svn.sourceforge.net/svnroot/pygccxml pygccxml
Then go to the subdirectories and find
Run Python setup. py install
PY ++ example:
Let's use the simplest code for example. More detailed functions need to be further studied:
Suppose we have two chg. h chg. CC files. We want to encapsulate the interfaces as Python callable interfaces.
Chg. h
# Ifndef chg _# DefineChg _ # include <iostream> # include <vector> # include <String>Using NamespaceSTD;ClassChg {Public: Chg (): m_age (28) {m_friend.push_back ("Mm");}IntGetage (){ReturnM_age ;}IntFriendsize (){ReturnM_friend.size ();}IntM_age; vector <String> M_friend ;};VoidHaha ();# Endif
Chg. CC
# Include"Chg. H"VoidHaha () {cout <"HAHAHA"<Endl ;}
We need to write a Python script to automatically complete encapsulation. If you do not want to perform further careful settings, such as which interfaces are encapsulated and which are not encapsulated, you can write them as follows, you can even write a script to automatically generate the following code. Of course, you can refer to the Getting Started tutorial of Py ++ for detailed settings. Http://www.language-binding.net/pyplusplus/pyplusplus.html #
Import osimport sysfrom pyplusplus import module_buildermb = module_builder.module_builder_t (Files = ['Chg. H'] # Header file to be encapsulated, gccxml_path ='/Usr/local/bin/gccxml') Mb. build_code_creator (module_name ='Libchg _ py') # Name of the python module to be generated: MB. code_creator.user_defined_directories.append (OS. Path. abspath ('.') Mb. write_module (OS. Path. Join (OS. Path. abspath ('.'),'Chg _ Py. CC') # Name of the code file encapsulated by boost. Python to be generated
Run the above file and we can see that chg_py.cc has been generated. It looks like the following:
// This file has been generated by Py ++. # Include "Boost/python. HPP" # Include "Boost/Python/suite/indexing/vector_indexing_suite.hpp" # Include "Chg. H" Namespace BP = boost: Python; boost_python_module (libchg_py ){{ //: STD: vector <STD: String> Typedef BP: Class _ <STD: vector <STD :: String > Vector_less _ std_scope_string _ greater _ exposer_t; vector_less _ std_scope_string _ greater _ exposer_t vector_less _ std_scope_string _ greater _ exposer = vector_less _ std_scope_string _ greater _ exposer_t ( "Vector_less _ std_scope_string _ greater _" ); BP: Scope vector_less _ std_scope_string _ greater _ Scope (vector_less _ greater _ exposer); vector_less _ std_scope_string _ greater _ exposer. def (BP: vector_indexing_suite <: STD: vector <STD :: String >, True > ();} BP: Class _ <chg> ( "Chg" , BP: init <> (). Def ( "Friendsize" ,( Int (: Chg: *) (&: chg: friendsize). Def ( "Getage" ,( Int (: Chg: *) (&: chg: getage). def_readwrite ( "M_age" , & Chg: m_age). def_readwrite ( "M_friend" , & Chg: m_friend );{ //: Haha Typedef Void (* Haha_function_type) (); BP: def ( "Haha" , Haha_function_type (&: Haha ));}}
Yes, as you can see, the function of Py ++ is to automatically generate boost. Python code... Without py ++, You need to manually complete these tasks. py ++ is really life-saving.
After compiling chg. CC chg_py.cc, note that you need to link the boost. Python and Python dynamic link libraries. This is what I wrote in my cmakelist.
Add_library (chg_py shared chg_py.cc chg. CC)
Target_link_libraries (chg_py libboost_python.so libpython2.6.so)
Check the effect (note that the cmake will automatically generate libchg_py.so ):
In [1]: From libchg_py import *
In [2]: chg = chg ()
In [3]: chg. getage ()
Out [3]: 28
In [4]: Print Len (chg. m_friend)
1
In [5]: chg. m_friend [0]
Out [5]: 'mm'
In [6]: Haha ()
Hahaha
C ++ has a lot of large libraries, such as cgal libraries related to computational ry. Most of them have Python interfaces. Is this the case? I guess so... Haha