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