We can call a C function from Python program using the ctypes module.
It involves the following steps:
{CDLL_instance}.{function_name}({function_parameters})
.#include <stdio.h>
int square(int i) {
return i * i;
}
We have a simple C function that will return the square of an integer. I have saved this function code in the file named my_functions.c.
We can use the following command to create the shared library file from the C source file.
$ cc -fPIC -shared -o my_functions.so my_functions.c
>>> from ctypes import *
>>> so_file = "/Users/pankaj/my_functions.so"
>>> my_functions = CDLL(so_file)
>>>
>>> print(type(my_functions))
<class 'ctypes.CDLL'>
>>>
>>> print(my_functions.square(10))
100
>>> print(my_functions.square(8))
64
>>>
If you change the C program file, you will have to regenerate the shared library file.
The python default implementation is written in C programming and it’s called CPython. So it’s not very uncommon to use C functions in a python program. In this tutorial, we learned how to easily call C functions in a python program.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
Java and Python Developer for 20+ years, Open Source Enthusiast, Founder of https://www.askpython.com/, https://www.linuxfordevices.com/, and JournalDev.com (acquired by DigitalOcean). Passionate about writing technical articles and sharing knowledge with others. Love Java, Python, Unix and related technologies. Follow my X @PankajWebDev
Hi Pankaj, Is it possible to call functions in Linux c executable (not . so file) from python?
- Manomugdha Biswas
how can i run cmake commands using python. Eg: I have a folder with .c files in it and they are linked using CMakeList.txt I do compile them using “cmake -H. -Bbuild” commands I need to put the above command in python script and run the .py directly to build the c files.can anyone help me
- Keerthana
is it possible to connect EM-18 RFID Reader to raspberry pi direct without using RS232 USB adapter? Help me please Sir.
- Jean Baptiste
doesn’t work. Traceback (most recent call last): File “”, line 1, in TypeError: ‘LibraryLoader’ object is not callable
- m j
it doesnt return the intteger that I defined in C file, It does net get a value by using scanf in C program.d
- Armagan
What if my C file includes some other custom C library? It seems that the resulting .so file doesn’t include them.
- Mauro
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.