Embed Python code into a C + + program to write

Source: Internet
Author: User

Embed Python code into an instance written by a C + + program, embedded in Python

There are a few steps you need to take to embed Python in C + +.

Install the Python program so that you can use Python's header files and libraries

Add "Python.h" header file to the source file we wrote, and link to "Python**.lib" library (still not clear the library when it is still the library or export library, need to understand)

Master and understand some of the Python's C language APIs for use in our C + + programs

Some of the common C API functions

It is necessary to understand the **pyobject*** pointer before understanding the following function, and almost all of the objects in Python are indicated using this pointer.

Py_initialize () &&py_finalize ()

The function to call before any Python C function is called, "py_initialize" is used to initialize the Python module, presumably to load the initialization load DLL. The corresponding "Py_finalize" is used to release the module after using the Python module.

Pyimport_importmodule ()

Used to load a Python module, which is a generic Python file. It is important to note that all the executable statements in the module will be executed when the module is loaded. Include import Imports statement and all statements outside the body of the function

Pyobject_getattrstring ()

Returns the function inside the module

Py_buildvalue ()

Set up a parameter tuple, usually using this function to create a tuple, and then pass the tuple as an argument to the function inside Python.

Pyeval_callobject ()

Call the function and pass the tuple established by "Py_buildvalue" as a parameter to the called function

Source instance

The following example is a function that calls Python in C + + code, passes a parameter, and gets the return value

Test.cpp Code

#include <iostream>

#include <Python.h>

using namespace Std;

int main (int argc, char* argv[])

{

Py_initialize (); Initialization

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)

Embed Python code into a C + + program to write

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.