How to embed Python into C/C++ for development

大家讲道理
Release: 2016-11-07 11:29:17
Original
1461 people have browsed it

If you want to embed Python into C/C++, it is relatively simple. What you need is to add Python’s 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 include directory in the Python installation directory to the include files item, and add the libs directory to the library files item.

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

The code is as follows:

An error occurred when executing under debug, "The python31_d.lib file cannot be found". The reason was later 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 &#39;hi, python!&#39;");  
Py_Finalize();  
return 0;  
}
Copy after login

Py_Initialize function prototype is: void Py_Initialize()

This function must be used when embedding Python into 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 successful.

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 open file pointer and filename is the python to be run. Script file name. 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 cause a 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 files

Py_BuildValue() is used for numbers and strings Conversion processing is performed into the corresponding data type 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 PyObject* type variables into 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++ An introduction to the relevant content. If you have any questions, please leave a message below.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template