How does Python embed C/C++ for development?

If you want to embed Python in C/C++ is relatively simple, what you need is to add Python include file directory and lib file directory in VC. Let's take a look at how to embed Python into C/C++.

Under VC6.0, open tools->options->directories->show directories for, add the inlude directory under the Python installation directory to the inlude files item, and add the libs directory to the library files item.

Under VC2005, open the tools->options->projects and solutions->VC++ directory, and then do the same work.

code show as below:

Execution error under debug, "Cannot find the python31_d.lib file", and the reason is found out: The python31_d.lib file must be generated under debug, otherwise it can only be generated under release

#include <python.h> 
int main()  
{  
Py_Initialize();  
PyRun_SimpleString("Print 'hi, python!'");  
Py_Finalize();  
return 0;  
}

The prototype of the Py_Initialize function is: void Py_Initialize()

This function must be used when embedding Python in C/C++. It initializes the Python interpreter and must be called before using other Python/C APIs. You can use the Py_IsInitialized function to determine whether the initialization is successful, and return True if it succeeds.

The prototype of the PyRun_SimpleString function is int PyRun_SimpleString(const char *command), which is used to execute a piece of Python code.

Note: Do you need to maintain the indentation between statements?

The prototype of the Py_Finalize function is void Py_Finalize(), which is used to close the Python interpreter and release the resources occupied by the interpreter.

The PyRun_SimpleFile function can be used to run ".py" script files. The function prototype is as follows:

int PyRun_SimpleFile(FILE *fp, const char *filename);

Where fp is the pointer of the opened file, and filename is the name of the python script file to be run. However, since the official release of this function is compiled by visual studio 2003.NET, if other versions of the compiler are used, the FILE definition may crash due to version reasons. At the same time, for simplicity, you can use the following method to replace this function:

PyRun_SimpleString("execfile('file.py')"); //Use execfile to run python file

Py_BuildValue() is used to convert numbers and strings into the corresponding data types in Python (in C language, all Python types are declared as PyObject types). The function prototype is as follows:

PyObject *Py_BuildValue(const char *format, …..);

PyString_String() is used to convert a variable of PyObject* type into a char* type that can be processed by C language. The specific prototype is as follows:

char* PyString_String(PyObject *p);

The above is an introduction to how to embed Python into C/C++. I hope it can help you learn.

end:

I am a python development engineer, and I have compiled a set of the latest python system learning tutorials, including basic python scripts to web development, crawlers, data analysis, data visualization, machine learning, and interview books. Those who want these materials can pay attention to the editor, add Q skirt 851211580 to pick up Python learning materials and learning videos, and online guidance from the Great God!

 

Guess you like

Origin blog.csdn.net/pyjishu/article/details/105434824