9
I want to use a function made in C
.
Example:
I have a function
int swap(){
printf("lista");
}
And I want to call you by Python...
9
I want to use a function made in C
.
Example:
I have a function
int swap(){
printf("lista");
}
And I want to call you by Python...
9
The first thing you will have to do is generate a dynamic library with your C code. On Linux this means a bilioteca ". so" and in windows would be a ". dll"
Assuming you are on Linux and your C file name is "hello. c", gcc can generate this dynamic library through following command:
gcc -shared -o libhello.so -fPIC hello.c
Now, to call this Python library you can use the library ctypes, which is part of the standard Python library:
import ctypes
libhello = ctypes.cdll.LoadLibrary("./libhello.so")
libhello.swap()
For more advanced examples, where the function receives or returns parameters, you will need to convert the Python format data to C format. See the ctypes documentation for details.
Browser other questions tagged c python python-3.x
You are not signed in. Login or sign up in order to post.