Home > Backend Development > Python Tutorial > Detailed explanation of how Python calls C/C++ underlying libraries and transfers values ​​to each other

Detailed explanation of how Python calls C/C++ underlying libraries and transfers values ​​to each other

高洛峰
Release: 2017-02-20 10:32:43
Original
1641 people have browsed it

As a script interpretation language, Python itself is well integrated with C++, so using Python to develop and call the C/C++ underlying library where performance requirements are required is simply an artifact. This article introduces in detail the problem of Python calling C/C++ underlying libraries and passing values ​​to each other. Let’s take a look.

Preface

Development environment:

Centos 7 + Python 3.5.1 + Qt Creator (just compiled with Qt Creator, and does not use any libraries of QT)

Python calls C/C++ libraries, there are two ways I can do it now

1.extern "C" export (It is troublesome to pass values ​​to each other, this method is not recommended):

Make the C/C++ library the same DLL or .so as usual, for example:

//.h文件
#include <Python.h>
//.cpp文件
//C/C++ my.so 或者my.dll
enter "C" void printHello()
{
  std::cout<<"Hello World"<<std::endl;
}
Copy after login

#Python
import ctypes 
from ctypes import *
loadso = ctypes.cdll.LoadLibrary 
mylib = loadso("./my.so")
mylib.printHello()
>>>Hello world
Copy after login

Code explanation:

my.so There is a C export functionprintHello()

import ctypes : Import an official library, as the name suggests, it is related to C

loadso = ctypes.cdll.LoadLibrary : loadso represents the function used to load the library

mylib = loadso(“./my.so”)  //Or loadso(“my.dll”) Load my.so library

mylib.printHello(): Call Library function

The above code can output normally: Hello World, but they do not pass values ​​to each other

Python and C++ pass values ​​to each other

//.h文件
#include <Python.h>
//.cpp文件
enter "C" int printHello(const char* str)
{
  std::cout<<str<<std::endl;
  return 1;  
}
Copy after login

Then the problem with Python comes

str = create_string_buffer(b"Hello World")
#mylib.printHello("Hello World") 这里死活就是显示:H,*(str+4)才是&#39;e&#39;,*(str+8) 是&#39;l&#39; 依次类推
print (mylib.printHello(str))
>>>Hello World
>>>1
#由于对Python不是特别的熟悉 怎么也做不到显示C++返回的字符串, Python只能显示C++返回的字符串子能看到一个地址而已
Copy after login

2.Python extension C/C++

Not much to say, just go to the code

//.h文件 本来这是C++连接Mysql 我只摘抄部分代#include <Python.h>
//.cpp文件
//传递多个参数 Python传过来的参数在args里面
PyObject* printfHello(PyObject* self,PyObject* args)
{
  int i=0
   const char* str;
  if (!PyArg_ParseTuple(args, "i|s", &i,&str))   //i 表示整形 s 表示字符串
    return PyLong_FromLong(0);
  print("%d,%s",i,str);
  return Py_BuildValue("s","OK");  //向Python返回OK字符串
}
//映射 知道MFC的一看就懂
static PyMethodDef MyMethods[] = {
{"printfHello", printfHello, METH_VARARGS,  //"printHello" 中可调用的函数 METH_VARARGS :带有参数   METH_NOARGS:无参数
"print"},   //说明
{"connect", connect, METH_VARARGS,
"connect mysql"},
{NULL, NULL, 0, NULL}
};
static PyObject* UtilError;
// 向Python中注册模块
static struct PyModuleDef spammodule = { 
PyModuleDef_HEAD_INIT,
"libMysqlUtil", //模块名字 import libMysqlUtil
"C++ Connect Mysql",
-1,
MyMethods
};//PyInit_libMysqlUtil 注意名字 一定要PyInit_ 加上你的模块名字 不然Python import 会提示没有定义 PyInit_你的模块名字 PyMODINIT_FUNC PyInit_libMysqlUtil(void) { PyObject* m = nullptr; m = PyModule_Create(&spammodule);
//m= Py_InitModule(....) Python 2.7 if(!m) { return m; } UtilError = PyErr_NewException("Util.error",NULL,NULL); Py_INCREF(UtilError); PyModule_AddObject(m,"error",UtilError); return m; }
Copy after login

#python
import libMysqlUtil
libMysqlUtil.printHello(1,"hello World")
>>>1,hello World
>>>OK
Copy after login

Summary

So far, Python and C/C++ communicate with each other and can meet most needs. Structure value transfer has not been studied yet. For classes, just use pointers. , there is a pointer in C++, and the pointer is converted into an integer in Python. When Python passes this integer to C++, it uses PyArg_ParseTuple to turn the integer into a class pointer.

For more detailed explanations of how Python calls C/C++ underlying libraries and transfers values ​​to each other, please pay attention to the PHP Chinese website!

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