Installation, version, and precautions of boost. Python and Python-dev Ubuntu 8.10 (7)

Source: Internet
Author: User
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.

  1. $ LD_LIBRARY_PATH =/home/LHB/local2.5.1/lib/home/LHB/local2.5.1/bin/Python
  2. Python 2.5.1 (r5.1: 54863, Dec 8 2008, 21:22:27)
  3. [GCC 4.3.2] On linux2
  4. Type "help", "Copyright", "Credits" or "License" for more information.
  5. >>> Import sys
  6. >>> SYS. maxunicode
  7. 1114111
  8. >>>

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

  1. # Include <python. h>
  2. # Include <boost/python. HPP>
  3. Using namespace boost: Python;
  4. Char const * greet ()
  5. {
  6. Return "Hello, world ";
  7. }
  8. Boost_python_module (Hello)
  9. {
  10. Def ("Greet", greet );
  11. }
  12. Int run1 ()
  13. {
  14. Py_initialize ();
  15. Inithello ();
  16. Pyrun_simplestring ("Import sys ");
  17. Pyrun_simplestring ("print sys. Version ");
  18. Pyrun_simplestring ("print sys. Path ");
  19. Pyrun_simplestring ("From time import time, ctime ");
  20. Pyrun_simplestring ("Print 'Today is ', ctime (Time ())");
  21. Pyrun_simplestring ("Import hello ");
  22. Pyrun_simplestring ("Print hello. Greet ()");
  23. Py_finalize ();
  24. Return 0;
  25. }
  26. Int run2 ()
  27. {
  28. Py_initialize ();
  29. Try
  30. {
  31. Inithello ();
  32. Const char * cmd = "Import sys/N "/
  33. "Print sys. Version/N "/
  34. "SYS. Path. append ('/home/LHB/local2.5.1/lib/python2.5')/n "/
  35. "Print sys. Path/N "/
  36. "Import mytest/N "/
  37. "Mytest. testimport ()/n "/
  38. "From time import time, ctime/N "/
  39. "Print 'Today is ', ctime (Time ()/n "/
  40. "Import Hello/N "/
  41. "Print hello. Greet ()";
  42. // Retrieve the main module
  43. Object main = import ("_ main __");
  44. // Retrieve the main module's namespace
  45. Object global (main. ATTR ("_ dict __"));
  46. // Define the derived class in Python.
  47. Object result = exec (CMD, global, global );
  48. }
  49. Catch (error_already_set)
  50. {
  51. Pyerr_print ();
  52. }
  53. Py_finalize ();
  54. Return 0;
  55. }
  56. Int main ()
  57. {
  58. Run1 ()
  59. Run2 ()
  60. }

Compile:

  1. #! /Bin/bash
  2. Boosthead = '/home/LHB/local/include/boost-1_37'
  3. Boostlib = '/home/LHB/local/lib'
  4. Pythonhead = '/home/LHB/local2.5.1/include/python2.5'
  5. Pythonlib = '/home/LHB/local2.5.1/lib'
  6. G ++-O seraphsf-seraph. cpp-L $ {pythonlib}-lpython2.5-L $ {boostlib}-lboost_python-mt-I $ {pythonhead}-I ${boosthead}-darch32
  7. #-Xlinker-export-dynamic
  8. LD_LIBRARY_PATH =$ {boostlib }:$ {pythonlib} LDD seraph251
  9. 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.