Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to introduce the application function of Python embedded Cramp C+

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

How to introduce how to introduce Python embedded Cramp C+ application function, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.

Python embedded in C ~ +

By embedding Python in Python +, you can use the powerful features provided by Python. By embedding Python, you can replace the interface in the form of a dynamic link library, so that you can easily modify the script code as needed without having to recompile the link binary dynamic link library.

High-level embedding Python uses Python/C API to embed Python at a higher level. The so-called high-level embedding mainly refers to no interaction between the program and the script. Create an empty "Win32 Console Application" in VC++ 6.0and create a new C source file in the project. Add the code shown below to it.

# include int main () {Py_Initialize (); / *

Python interpreter initialization

* / PyRun_SimpleString ("print'hijima pythonium'"); / *

Run the string * / Py_Finalize (); / * end the Python interpreter and release resources

* / return 0;}

The project needs to be compiled when Python is embedded in Chand C+. After running the program, the output is shown below.

Hi,python!

You can see that the program is very simple, using only three functions. The prototype of the Py_Initialize function is shown below.

Void Py_Initialize ()

You must use this function when embedding a Python script, which initializes the Python interpreter. The Py_Initialize function must be called before using another Python/C API. The PyRun_SimpleString function is used to execute a piece of Python code. The function prototype is shown below.

Int PyRun_SimpleString (const char * command)

The Py_Finalize function is used in the program, and the prototype is shown below.

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 shown below.

Int PyRun_SimpleFile

(FILE * fp, const char * filename)

The meaning of the parameters is as follows. Fp: open file pointer. Filename: the file name of the Python script to run. You need to be aware of the compiler version you are using when using this function under Windows. Because the official release of Python is compiled by Visual Studio 2003.NET. If you use another version of the compiler, the definition of FILE varies due to version differences, so using other versions of the compiler will cause the program to crash.

For simplicity, you can use the following instead of the PyRun_SimpleFile function to achieve the same function.

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

Run the Python script file using execfile.

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report