Method analysis of Python calling C language program

First, we need to compile the written c program file into a dynamic library file (the extension is .so)

Use the following command:

gcc original file name.c -shared -o new file name.so

Then import the module in the python program: from ctypes import *

Introduce the c dynamic library in python, and define a variable to receive the dynamic library:

a=cdll.LoadLibrary("dynamic library file path")

Then you can call the functions in it.

a. Function name ()

Specific usage examples are as follows:

//c language code

# include<stdio.h 

voidfun(){printf("hello world\n");}
# python code

from ctypes import*
res=cdll.LoadLibrary('main.so')
res.fun()

The execution result is shown in the figure below:

The above is the whole content of this article, I hope it will be helpful to everyone's study.

Recommended Posts

Method analysis of Python calling C language program
Python analysis of wav files
Analysis of JS of Python crawler
Python implementation of gomoku program
Analysis of Python Sandbox Escape
Analysis of Python object-oriented programming
Python method of parameter passing
Summary of logarithm method in Python
Analysis of usage examples of Python yield
Analysis of Python conditional control statements
Summary of Python calling private attributes
​Full analysis of Python module knowledge
End the method of running python
Basic analysis of Python turtle library implementation
Detailed analysis of Python garbage collection mechanism
Ubuntu19.10 compile and run C language program
Analysis of glob in python standard library
Method of installing django module in python
The essence of Python language: Itertools library
What are the advantages of python language
The specific method of python instantiation object
Example method of reading sql from python
Analysis of common methods of Python multi-process programming
The specific method of python import library
Python replacement pip source method process analysis
Detailed explanation of the principle of Python super() method
Analysis of common methods of Python operation Jira library
Python implementation of AI automatic matting example analysis
7 features of Python3.9
2.3 Python language foundation
Python data analysis
What is the advantage of python over corporate language