Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are python extensions shared libraries? Is it possible to make a static-linked library?

Tags:

python

c

I'm an extension noob. What I want to do is create an extension that doesn't require other libraries to be installed. Is this impossible because the extension has to link against a specific version of libpython at runtime?

like image 310
amoffat Avatar asked May 19 '10 13:05

amoffat


People also ask

What is the difference between shared library and static?

Static libraries, while reusable in multiple programs, are locked into a program at compile time. Dynamic, or shared libraries, on the other hand, exist as separate files outside of the executable file.

Why do we need shared libraries in addition to static ones?

The most significant advantage of shared libraries is that there is only one copy of code loaded in memory, no matter how many processes are using the library. For static libraries each process gets its own copy of the code. This can lead to significant memory wastage.

What is the extension for a static library?

To create such a library, the exported functions/procedures and other objects variables must be specified for external linkage (i.e. by not using the C static keyword). Static library filenames usually have ". a" extension on Unix-like systems and " . lib" extension on Microsoft Windows.

Can python import static library?

You can't do this. You have two options: Recompile the library as a shared library. Then use ctypes to call methods from the dynamically-loaded shared library.


1 Answers

You can't make a statically linked extension module because Python needs to load it dynamically at runtime and because (as you reasoned) the module needs to dynamically link against libpython.

You could compile your own custom version of Python with your extension statically linked into the interpreter. That's usually more trouble than it's worth.

Why do you want to make a statically linked extension? If we have more information about your goals, we might be able to help you achieve them in a different way.

Welcome to StackOverflow. :-)

like image 182
Daniel Stutzbach Avatar answered Sep 21 '22 12:09

Daniel Stutzbach