Introduction to the powerful functions generated when Python is embedded into C/C ++

Source: Internet
Author: User

If you embed Python into C/C ++, you will understand the powerful functions of Python in actual applications. Embedding Python can replace related interfaces in the form of dynamic link libraries, this not only makes it easy to modify the script code as needed, but does not need to re-compile the dynamic link library that links the binary.

High-level embedded Python

Python/C APIs can be used to embed Python at a higher level. The so-called high-level embedding mainly refers to the absence of interaction between programs and scripts. Create a blank "Win32 Console Application" in VC ++ 6.0 and a new C source file in the project. Add the following code to it.

 
 
  1. #include <Python.h> 
  2. int main()  
  3. {  

Py_Initialize ();/* initialize the Python interpreter */

PyRun_SimpleString ("print 'Hi, python! '");/* Run the string */

Py_Finalize ();/* ends the Python interpreter and releases resources */

 
 
  1. return 0;  
  2. }  
  3.  

Compile the project and run the program. The output is as follows.

 
 
  1. hi,python! 

We can see that the program is very simple and only three functions are used. The prototype of the Py_Initialize function is as follows.

 
 
  1. void Py_Initialize() 

This function must be used when Python is embedded in C/C ++. It initializes the Python interpreter. You must call the Py_Initialize function before using other Python/C APIs. The PyRun_SimpleString function is used to execute a piece of Python code. The function prototype is as follows.

 
 
  1. int PyRun_SimpleString(const char *command) 

The Py_Finalize function is used at the end of the program. Its prototype is as follows.

 
 
  1. void Py_Finalize() 

The Py_Finalize function is used to close the Python interpreter and release the resources occupied by the interpreter.

In addition to using the PyRun_SimpleString function, you can also use the PyRun_SimpleFile () function to run the ". py" script file. The function prototype is as follows.

 
 
  1. int PyRun_SimpleFile( FILE *fp, const char *filename) 

The parameter meanings are as follows.

· Fp: Open File pointer.

· Filename: name of the Python script file to be run.

When using this function in Windows, pay attention to the version of the compiler used. Python officially released is compiled by Visual Studio 2003. NET. If a compiler of another version is used, FILE definitions are different due to version differences. Using a compiler of another version causes program crashes.

For convenience, you can use the following method to replace the PyRun_SimpleFile function to implement the same function.

 
 
  1. PyRun_SimpleString("execfile('file.py')"); 

Run a Python script file using execfile

The above section describes how to embed Python into C/C ++ and how to use the powerful functions provided by Python. the above article mainly introduces the content related to C/C ++ high-level embedded Python, hope you will have some gains.

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.