How python calls c or c++ library

Python can call the C/C++ library in the following ways:

  • Using the ctypes module
    This is a module built into Python that allows you to directly call C functions defined in .h header files.

Example:

import ctypes
libc = ctypes.CDLL('libc.so.6')  # Load the C library
libc.printf()  # Call a C function
  • Using Cython
    Cython is an open source Python and C hybrid language. You can compile the Cython source code into a C extension, and then load and use it in Python.
    Example:
# test.pyx
cdef extern from "header.h"

Guess you like

Origin blog.csdn.net/zhangzhechun/article/details/131852069
Recommended