Have you ever came across the situation where you have to call C function using python? This article is going to help you on very basic level and if you have not come across any situation like this, you enjoy knowing how it is possible.
First, let’s write one simple function using C and generate shared library of the file. Let’s say file name is function.c.
Compile this :
cc -fPIC -shared -o libfun.so function.c
Using ctypes(foreign function interface) library to call C function from Python
Above statement will generate a shared library with name libfun.so. Now, let’s see how to make use of it in python. In python, we have one library called ctypes. Using this library we can use C function in python.
Let’s say file name is function.py.
- Python - Call function from another file
- Decorator to print Function call details in Python
- Is Python call by reference or call by value
- Python - Call function from another function
- call() decorator in Python
- Python: Call Parent class method
- Wand function() function in Python
- Python map() function
- Python | now() function
- Python | hex() function
- Python | dir() function
- Help function in Python
- Python | oct() function
- Python | int() function
- Python | cmp() function
- sum() function in Python
- Python tell() function
- ord() function in Python
- Python str() function
- id() function in Python
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.