I have introduced some examples of Python and C ++ interoperability.
Series of articles:
Python calls C/C ++ functions (1)
Python calls C ++ (2) encapsulated by boost Python)
C ++ calls Python (3)
C ++ calls Python (4)
C ++ and Python advanced application interoperability (5)
C ++ calls the pythonapi thread status and global interpretation lock (6)
It is very prone to problems, such as Unicode problems and importerror problems. So let's introduce the installation and configuration.
1. Install Python-Dev
Http://www.python.org/
Download 2.5.1/2.5.2/2.6.1.
TK-Dev, TCL-Dev, and zlib are required.
Extract
$./Configure -- prefix =/home/LHB/local2.5.1 -- enable-Unicode = ucs4 -- enable-shared
Enable-UNICODE: ucs2 or ucs4. Generally, ucs4 is used in Linux.
Enable-shared: Compile the shared library.
$ Make
$ Make install
After the installation is complete, check whether ucs4 is supported. Many people see the pyunicode error because Python does not support ucs4.
- $ LD_LIBRARY_PATH =/home/LHB/local2.5.1/lib/home/LHB/local2.5.1/bin/Python
- Python 2.5.1 (r5.1: 54863, Dec 8 2008, 21:22:27)
- [GCC 4.3.2] On linux2
- Type "help", "Copyright", "Credits" or "License" for more information.
- >>> Import sys
- >>> SYS. maxunicode
- 1114111
- >>>
If it is 1114111, It is ucs4.
For Python Unicode articles, refer to here
Note: If you use the newly installed python, You need to modify the environment variables, such as export pythonhome =/home/LHB/local2.5.1/. Otherwise, importerror may occur: /usr/lib/python2.5/lib-dynload/math. so: Undefined Symbol: pyfpe_jbuf. The cause of this problem is that the two paths in your python are inconsistent.
2. Install boost. Python
Three packages may be required
Zlib
Http://www.zlib.net/
Http://www.zlib.net/zlib-1.2.3.tar.gz
Note: Compile the shared library./configure -- shared
Bzlib
Http://www.bzip.org/
Http://www.bzip.org/1.0.5/bzip2-1.0.5.tar.gz
Libicu
Http://www.icu-project.org/download/
Boost versions 35 and 17
Http://www.boost.org/
Extract
Configuration
$./Configure -- prefix =/home/LHB/local -- With-ICU -- With-libraries = Python
With-libraries = python is only compiled for python, And the Compilation Time is terrible!
$ Make
$ Make install
3. Example
- # Include <python. h>
- # Include <boost/python. HPP>
- Using namespace boost: Python;
- Char const * greet ()
- {
- Return "Hello, world ";
- }
- Boost_python_module (Hello)
- {
- Def ("Greet", greet );
- }
- Int run1 ()
- {
- Py_initialize ();
- Inithello ();
- Pyrun_simplestring ("Import sys ");
- Pyrun_simplestring ("print sys. Version ");
- Pyrun_simplestring ("print sys. Path ");
- Pyrun_simplestring ("From time import time, ctime ");
- Pyrun_simplestring ("Print 'Today is ', ctime (Time ())");
- Pyrun_simplestring ("Import hello ");
- Pyrun_simplestring ("Print hello. Greet ()");
- Py_finalize ();
- Return 0;
- }
- Int run2 ()
- {
- Py_initialize ();
- Try
- {
- Inithello ();
- Const char * cmd = "Import sys/N "/
- "Print sys. Version/N "/
- "SYS. Path. append ('/home/LHB/local2.5.1/lib/python2.5')/n "/
- "Print sys. Path/N "/
- "Import mytest/N "/
- "Mytest. testimport ()/n "/
- "From time import time, ctime/N "/
- "Print 'Today is ', ctime (Time ()/n "/
- "Import Hello/N "/
- "Print hello. Greet ()";
- // Retrieve the main module
- Object main = import ("_ main __");
- // Retrieve the main module's namespace
- Object global (main. ATTR ("_ dict __"));
- // Define the derived class in Python.
- Object result = exec (CMD, global, global );
- }
- Catch (error_already_set)
- {
- Pyerr_print ();
- }
- Py_finalize ();
- Return 0;
- }
- Int main ()
- {
- Run1 ()
- Run2 ()
- }
Compile:
- #! /Bin/bash
- Boosthead = '/home/LHB/local/include/boost-1_37'
- Boostlib = '/home/LHB/local/lib'
- Pythonhead = '/home/LHB/local2.5.1/include/python2.5'
- Pythonlib = '/home/LHB/local2.5.1/lib'
- G ++-O seraphsf-seraph. cpp-L $ {pythonlib}-lpython2.5-L $ {boostlib}-lboost_python-mt-I $ {pythonhead}-I ${boosthead}-darch32
- #-Xlinker-export-dynamic
- LD_LIBRARY_PATH =$ {boostlib }:$ {pythonlib} LDD seraph251
- LD_LIBRARY_PATH =$ {boostlib }:$ {pythonlib}./seraph251
Note:: This option is-xlinker-export-dynamic. The enable-shared option is configured in Python. If no data is written, the dynamic library is not compiled, and only the static library is used. In this case, you need to add-xlinker-export-dynamic when compiling the example you wrote. Otherwise, the following problem occurs during execution: importerror: /usr/lib/python2.5/lib-dynload/time. so: Undefined Symbol: pymodule_addobject
Note: many people may not be clear about the relationship between python, Python-Dev, and boost. Python. Python is the execution environment. If you want to call python in C/C ++, or Python to call C or C ++, you need Python-Dev. Boost. python only encapsulates Python-Dev. Many functions have the same functions on both sides. That is to say, Python-Dev can also be used, such as run1 function and boost. in python, such as run2 and boost. python interfaces are encapsulated more friendly, such as exception handling.