Python embeds C/C++ for detailed development

If you want to embed Python in C/C++ is a relatively simple thing, 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 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();return0;}

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. This function 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 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 the ".py" script file. 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 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 the version. 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 PyObject* type variables into char* types that can be processed by C language. The specific prototype is as follows:

char* PyString_String(PyObject *p)。

Knowledge point expansion:

The method of Python calling C/C++ DLL dynamic link library

First, create a DLL project (in this case, the creation environment is VS 2005), the header file:

//hello.h
# ifdef EXPORT_HELLO_DLL
# define HELLO_API __declspec(dllexport)
# else
# define HELLO_API __declspec(dllimport)
# endif
extern "C"{
 HELLO_API int IntAdd(int , int);}

CPP file:

//hello.cpp
# define EXPORT_HELLO_DLL
# include "hello.h"
HELLO_API int IntAdd(int a, int b){return a + b;}

There are two points to note here:

(1) Find out whether __cdecl or __stdcall is used in the function calling convention when compiling, because according to the function calling convention in the DLL, Python will use the corresponding function to load the DLL.

(2) If a C++ project is used, the exported interface needs to be extern "C", so that the exported function can be recognized in python.

So far, this article on the detailed explanation of Python embedding C/C++ for development is introduced. For more related Python how to embed C/C++ for development content, please search for ZaLou.Cn's previous articles or continue to browse related articles below. Hope everyone Support ZaLou.Cn a lot in the future!

Recommended Posts

Python embeds C/C++ for detailed development
Detailed tutorial on installing python3.7 for ubuntu18
14. MysSQL for Python3
Python3 development environment to build a detailed tutorial
Detailed explanation of the implementation steps of Python interface development
python development [first article]
Detailed Python IO programming
Detailed explanation of common tools for Python process control
Detailed Python loop nesting
Detailed steps for installing Django under Python 3.6 in Ubuntu 16.04 environment
python opencv for image stitching
ubuntu build python development environment
Selenium visual crawler for python crawler
Python3 interface development commonly used.md