As a script interpretation language, Python is a good combination of C ++. Therefore, using Python for development and calling the CC ++ underlying library where performance requirements are required, this is simply an artifact. This article describes in detail the issue of passing values to each other by calling the CC ++ underlying library in Python. let's take a look at it. Python, as a script interpretation language, is a good combination of C ++. Therefore, it is an artifact to use Python for development to call the C/C ++ underlying library where performance requirements are required. This article describes in detail how to call the C/C ++ underlying library in Python and transfer values to each other. let's take a look at it.
Preface
Development Environment:
Centos 7 + Python 3.5.1 + Qt Creator (only compiled using Qt Creator, and no QT library is used)
Python calls the C/C ++ library. I can do this in two ways:
1. extern "C" export(It is troublesome to pass values to each other. we do not recommend this method ):
Make the C/C ++ library into the same DLL and. so as usual, for example:
//. H file # include
//. Cpp file // C/C ++ my. so or my. dllenter "C" void printHello () {std: cout <"Hello World" <
#Pythonimport ctypes from ctypes import *loadso = ctypes.cdll.LoadLibrary mylib = loadso("./my.so")mylib.printHello()>>>Hello world
Code explanation:
My. soThere is a C export functionprintHello()
Import ctypes:Import an official library, which is related to C as the name suggests
Loadso = ctypes. cdll. LoadLibrary:Loadso indicates the function used to load the database.
Mylib = loadso ("./my. so ")// Or loadso ("my. dll") to load the my. so Library
Mylib. printHello ():Call library functions
The above code can be output normally: Hello World, but they do not pass values to each other
Python and C ++ pass values to each other
//. H file # include
// Enter "C" int printHello (const char * str) {std: cout <
So the Python issue comes.
Str = create_string_buffer (B "Hello World") # mylib. printHello ("Hello World"): H, * (str + 4) is 'e', * (str + 8) is 'L' and print (mylib. printHello (str) >>> Hello World >>> 1 # as you are not familiar with Python, you cannot display the strings returned by C ++, python can only display an address in the string returned by C ++.
2. Python extension C/C ++
Not to mention, directly add the code
//. H file. Originally, this is a C ++ connection to Mysql. I only extract some replicas # include
//. Cpp file // pass parameters passed by Python in the args PyObject * printfHello (PyObject * self, PyObject * args) {int I = 0 const char * str; if (! PyArg_ParseTuple (args, "I | s", & I, & str) // I indicates the integer. s indicates the string return PyLong_FromLong (0); print ("% d, % s ", I, str); return Py_BuildValue (" s "," OK "); // return an OK string to Python} // static ing understand static PyMethodDef MyMethods [] = {"printfHello", printfHello, METH_VARARGS, // The METH_VARARGS function that can be called in "printHello": with the METH_NOARGS parameter: no parameter "print"}, // description {"connect", connect, METH_VARARGS, "connect mysql "}, {NULL, NULL, 0, NULL}; static Py Object * UtilError; // register the module static struct PyModuleDef spammodule = {PyModuleDef_HEAD_INIT, "libMysqlUtil", // The module name is import libMysqlUtil "C ++ Connect Mysql",-1, myMethods}; // PyInit_libMysqlUtil be sure to add your module name to PyInit _. Otherwise, the Python import will prompt that PyInit_libMysqlUtil (void) is not defined) {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 ;}
#pythonimport libMysqlUtillibMysqlUtil.printHello(1,"hello World")>>>1,hello World>>>OK
Summary
So far, Python and C/C ++ can communicate with each other to meet most of the requirements. The structure has not been studied. for classes, use pointers. in C ++, pointers are used, in Python, the pointer is converted into an integer. When Python passes the integer to C ++, PyArg_ParseTuple is used to convert the integer into a class pointer.
For more details about how to call the C/C ++ underlying library and pass values to each other in python, please follow the PHP Chinese network!