search
HomeBackend DevelopmentPython TutorialDetailed explanation of how Python calls C/C++ underlying libraries and transfers values ​​to each other

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;
}

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

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;  
}

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++返回的字符串子能看到一个地址而已

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; }

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

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!

Statement
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
How are arrays used in scientific computing with Python?How are arrays used in scientific computing with Python?Apr 25, 2025 am 12:28 AM

ArraysinPython,especiallyviaNumPy,arecrucialinscientificcomputingfortheirefficiencyandversatility.1)Theyareusedfornumericaloperations,dataanalysis,andmachinelearning.2)NumPy'simplementationinCensuresfasteroperationsthanPythonlists.3)Arraysenablequick

How do you handle different Python versions on the same system?How do you handle different Python versions on the same system?Apr 25, 2025 am 12:24 AM

You can manage different Python versions by using pyenv, venv and Anaconda. 1) Use pyenv to manage multiple Python versions: install pyenv, set global and local versions. 2) Use venv to create a virtual environment to isolate project dependencies. 3) Use Anaconda to manage Python versions in your data science project. 4) Keep the system Python for system-level tasks. Through these tools and strategies, you can effectively manage different versions of Python to ensure the smooth running of the project.

What are some advantages of using NumPy arrays over standard Python arrays?What are some advantages of using NumPy arrays over standard Python arrays?Apr 25, 2025 am 12:21 AM

NumPyarrayshaveseveraladvantagesoverstandardPythonarrays:1)TheyaremuchfasterduetoC-basedimplementation,2)Theyaremorememory-efficient,especiallywithlargedatasets,and3)Theyofferoptimized,vectorizedfunctionsformathematicalandstatisticaloperations,making

How does the homogenous nature of arrays affect performance?How does the homogenous nature of arrays affect performance?Apr 25, 2025 am 12:13 AM

The impact of homogeneity of arrays on performance is dual: 1) Homogeneity allows the compiler to optimize memory access and improve performance; 2) but limits type diversity, which may lead to inefficiency. In short, choosing the right data structure is crucial.

What are some best practices for writing executable Python scripts?What are some best practices for writing executable Python scripts?Apr 25, 2025 am 12:11 AM

TocraftexecutablePythonscripts,followthesebestpractices:1)Addashebangline(#!/usr/bin/envpython3)tomakethescriptexecutable.2)Setpermissionswithchmod xyour_script.py.3)Organizewithacleardocstringanduseifname=="__main__":formainfunctionality.4

How do NumPy arrays differ from the arrays created using the array module?How do NumPy arrays differ from the arrays created using the array module?Apr 24, 2025 pm 03:53 PM

NumPyarraysarebetterfornumericaloperationsandmulti-dimensionaldata,whilethearraymoduleissuitableforbasic,memory-efficientarrays.1)NumPyexcelsinperformanceandfunctionalityforlargedatasetsandcomplexoperations.2)Thearraymoduleismorememory-efficientandfa

How does the use of NumPy arrays compare to using the array module arrays in Python?How does the use of NumPy arrays compare to using the array module arrays in Python?Apr 24, 2025 pm 03:49 PM

NumPyarraysarebetterforheavynumericalcomputing,whilethearraymoduleismoresuitableformemory-constrainedprojectswithsimpledatatypes.1)NumPyarraysofferversatilityandperformanceforlargedatasetsandcomplexoperations.2)Thearraymoduleislightweightandmemory-ef

How does the ctypes module relate to arrays in Python?How does the ctypes module relate to arrays in Python?Apr 24, 2025 pm 03:45 PM

ctypesallowscreatingandmanipulatingC-stylearraysinPython.1)UsectypestointerfacewithClibrariesforperformance.2)CreateC-stylearraysfornumericalcomputations.3)PassarraystoCfunctionsforefficientoperations.However,becautiousofmemorymanagement,performanceo

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)