Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to take compiled code(like C) and use it as a python module

Tags:

python

c

say i have some code in C which is fast and specific but i want to manage how its called in python .i have seen python modules written in c like numpy how would i do that? and is this possible for all compiled languages like rust/go etc.

for example i have a really fast custom database orm in c++ with multithreading and all that good stuff. how would i convert it into a python library and use it? i would like this as python isn't as fast as or as versatile as C++ etc.

if this is possible how does it work in the back ?

like image 661
Rando Avatar asked Mar 12 '26 22:03

Rando


1 Answers

Here are the steps:

  • Write file in C
  • Compile and generate shared library (.so)
  • Load using ctypes
  • Call those methods

More detailed answer:

Write file in C:

// test.c
int add_numbers(int a, int b) {
    return a + b;
}

Compile and generate shared library:

gcc -shared -o test.so test.c

Load using ctypes:

import ctypes

lib = ctypes.CDLL('./test.so')

Call those methods (This step has the most overhead)

lib.add_numbers.argtypes = (ctypes.c_int, ctypes.c_int)
lib.add_numbers.restype = ctypes.c_int

result = lib.add_numbers(3, 4)
print(result)

Good read:

  • https://docs.python.org/3/library/ctypes.html
  • https://en.wikipedia.org/wiki/Application_binary_interface
  • Web search: NABI (Native Application Binary Interface)

NB: doing this for a DB will require lots of attention, and experiment; as you need to compile it. Trust me it takes lots of time. Then you need to add these method wrapper in Python.

Yes, you can do the opposite also, you need to have <Python.h> header in your program. But trust me, it's more work. But if it required for some reason, yeah there is atleast a way, of course.

like image 134
Maifee Ul Asad Avatar answered Mar 14 '26 11:03

Maifee Ul Asad



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!