An example of embedding Python code into a C ++ program.

Source: Internet
Author: User

An example of embedding Python code into a C ++ program.

Some steps are required to embed python into C ++.

Install the python program to use the python header file and library.
Add "Python. h "header file and link it to" python **. lib "library (it is not clear whether the static library is imported or exported to the database yet, so it should be clear)
Understand and understand some python c language APIs for use in our c ++ Program

Common c api functions

Before learning about the following functions, it is necessary to understand the ** PyObject ** pointer. Almost all objects in python are indicated by this pointer.
Py_Initialize () & Py_Finalize ()

The function that needs to be called before calling any python c function. "Py_Initialize" is used to initialize the python module. It is assumed that it loads the initialization and load dll. After using the python module, use "Py_Finalize" to release the module.

PyImport_ImportModule ()

It is used to load a python module, which is a common python file. Note that all statements that can be executed in the module are executed when this module is loaded. Including the import Statement and all statements other than the function body.

PyObject_GetAttrString ()

Return the functions in the module.

Py_BuildValue ()

Create a parameter tuples. This function is generally used to create tuples and pass the tuples as parameters to functions in python.

PyEval_CallObject ()

Call the function and pass the tuples created by "Py_BuildValue" as parameters to the called function.

Source code example

The following example calls Python functions in c ++ code, Passes parameters and obtains the returned values.

Test. cpp Code

# Include <iostream> # include <Python. h> using namespace std; int main (int argc, char * argv []) {Py_Initialize (); // initialize PyObject * pModule = NULL; PyObject * pFunc = NULL; pyObject * pParam = NULL; PyObject * pResult = NULL; const char * pBuffer = NULL; int iBufferSize = 0; pModule = PyImport_ImportModule ("test_python"); if (! PModule) {cout <"get module failed! "<Endl; exit (0);} pFunc = PyObject_GetAttrString (pModule," main "); if (! PFunc) {cout <"get func failed! "<Endl; cout <int (pFunc) <endl; exit (0);} pParam = Py_BuildValue (" (s) "," HEHEHE "); pResult = PyEval_CallObject (pFunc, pParam); if (pResult) {if (PyArg_Parse (pResult, "(si)", & pBuffer, iBufferSize )) {cout <pBuffer <endl; cout <iBufferSize <endl ;}} Py_DECREF (pParam); Py_DECREF (pFunc); Py_Finalize (); // cout <"hello" <endl; return 0 ;}

Test_python.py code

def main(szString):  return ("hello", 5)

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.