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:
- def PyAdd(x, y)
- nSum = x + y
- print "Sum = ", nSum
- return nSum
4. Calling the embedded Python script in C ++
4.1 introduce header files
Win32/linux needs to introduce the Python header file
- ifdef WIN32
- #include "Python.h"
- #else
- #include "/usr/src/Python-2.4/Include/Python.h"
- #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.
- #ifndef WIN32
- PyRun_SimpleString("import sys");
- PyRun_SimpleString("sys.path.append('./')");
- PyRun_SimpleString("import os");
- PyRun_SimpleString("import string");
- #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.
- int CallPyFunction(const char *pszModuleName,
- const char *pModulFuncName,
- const char *pParam[],
- const int nCount)
- {
- PyObject *pName = NULL;
- PyObject *pModule = NULL;
- PyObject *pDict = NULL;
- PyObject *pFunc = NULL;
- PyObject *pParams = NULL;
- PyObject *pCurrParam = NULL;
- int i = 0;
- pName = PyString_FromString(pszModuleName);
- pModule = PyImport_Import(pName);
- pDict = PyModule_GetDict(pModule);
- pFunc = PyDict_GetItemString(pDict, pModulFuncName);
- pParams = PyTuple_New(nCount);
- i = 0;
- while (i < nCount)
- {
- pCurrParam = PyString_FromString(pParam[i]);
- PyTuple_SetItem(pParams, i, pCurrParam);
- i++;
- }
- PyObject *pFtp= PyObject_CallObject(pFunc, pParams);
- return true;
- }
The above is an introduction to the content that embeds Python into C ++ to make up for some shortcomings of C ++.