Search code examples
pythonmacosgccdistutilsshared-objects

How to create make .so files from code written in C or C++ that are usable from Python


Looking at Python modules and at code in the "lib-dnyload" directory in the Python framework, I noticed whenever code is creating some kind of GUI or graphic it imports a non-Python file with a .so extension. And there are tons .so files in "lib-dnyload".

From googling things I found that these files are called shared objects and are written in C or C++. I have a Mac and I use GCC. How do I make shared object files that are accessible via Python? Mainly just how to make shared objects with GCC using Mac OS X.


Solution

  • In my opinion the easiest way is to use Cython. Cython will generate some C code for you, compile it to make a ".so" library that you can load from python. This is easy and painless. I suggest you to follow the tutorial and you should have a so library pretty soon.

    If you want to know all the details behind C extensions of python, you should dive in the Python documentation about C extensions.