How to embed Python into C ++ to make up for the shortcomings of C ++

Source: Internet
Author: User

C ++ has a lot of flaws in actual operations. If we embed Python into C ++, we can make up for some shortcomings of C ++, to improve program flexibility by embedding C ++ in Python, the following describes the actual operation steps.

1. Install Python

1.1 install Python on Windows:

Directly go to the relevant website to download the latest version of an installation package python-2.5.1.msi), double-click the installation file on it.

1.2 install Python on linux:

In linux development, download the original code, through the original code installation needs to execute configure, make, make install several steps, I download the installation is python-2.4.tgz.

2. Preliminary development work

In the win32 environment, add Lib and Include after Python installation to VC Directories. In the linux environment, Lib and Include are specified through gcc.

3. Compile a Python script

Writing Python functions is not difficult. You can use a text editor or IDLE (officially provided by Python ). The following is a simple python script for adding two numbers:

 
 
  1. def PyAdd(x, y)  
  2. nSum = x + y  
  3. print "Sum = ", nSum  
  4. return nSum 

4. Calling the embedded Python script in C ++

4.1 introduce header files

Win32/linux needs to introduce the Python header file

 
 
  1. ifdef WIN32  
  2. #include "Python.h"   
  3. #else  
  4. #include "/usr/src/Python-2.4/Include/Python.h"  
  5. #endif 

4.2 When embedding C ++ in Python to make up for some shortcomings of C ++ itself, C ++ needs to be initialized before calling Python. Before the program ends, close Python.

Initialize Python Py_Initialize ();

Disable Python Py_Finalize ();

4.3 In linux, the python library code must be introduced after Py_Initialize.

 
 
  1. #ifndef WIN32  
  2. PyRun_SimpleString("import sys");   
  3. PyRun_SimpleString("sys.path.append('./')");   
  4. PyRun_SimpleString("import os");  
  5. PyRun_SimpleString("import string");  
  6. #endif 

Specify the database to be introduced as needed.

4.4 C ++ after preparation for embedding Python, C ++ programming is completed. In order to call Python functions with good scalability, A CallPyFunction function is used to call Python functions and file names of Python scripts, function Name and parameters are passed out of CallPyFunction. The following code is the core code of CallPyFunction. The Code does not contain error handling.

 
 
  1. int CallPyFunction(const char *pszModuleName,  
  2. const char *pModulFuncName,  
  3. const char *pParam[],  
  4. const int nCount)  
  5. {  
  6. PyObject *pName = NULL;  
  7. PyObject *pModule = NULL;  
  8. PyObject *pDict = NULL;  
  9. PyObject *pFunc = NULL;  
  10. PyObject *pParams = NULL;  
  11. PyObject *pCurrParam = NULL;   
  12. int i = 0;  
  13. pName = PyString_FromString(pszModuleName);   
  14. pModule = PyImport_Import(pName);   
  15. pDict = PyModule_GetDict(pModule);  
  16. pFunc = PyDict_GetItemString(pDict, pModulFuncName);   
  17. pParams = PyTuple_New(nCount);  
  18. i = 0;  
  19. while (i < nCount)  
  20. {  
  21. pCurrParam = PyString_FromString(pParam[i]);   
  22. PyTuple_SetItem(pParams, i, pCurrParam);   
  23. i++;  
  24. }  
  25. PyObject *pFtp= PyObject_CallObject(pFunc, pParams);  
  26. return true;  
  27. }   

The above is an introduction to the content that embeds Python into C ++ to make up for some shortcomings of C ++.

Related Article

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.