I have a static C library, that I need to call into from Python. I was looking at ctypes for this however it can only work with dynamically loaded libraries. Is there an equivalent or alternative for staticly linked libraries?
-
2A static lib on its own is almost useless. It's supposed to be linked into an *ELF* (or *PE* on *Win*). – CristiFati Aug 12 '19 at 23:01
2 Answers
You can't; static libraries are for linking to make new executables or dynamic libraries and nothing else. But you may be able to create a dynamic library from a static one (more info here), depending on how the static library was compiled.

- 143,180
- 12
- 188
- 271
-
1They could do it by building CPython with the static-library-using extension as a built-in, I believe. But I'm inclined to guess that they are not prepared to go that far. – John Bollinger Aug 12 '19 at 23:13
-
@JohnBollinger: Yes, they could make an actual Python extension by whatever means are appropriate; my answer was the minimal work answer to "make a static library usable from Python", but the broad answer is "it needs to be something other than a static library" to be usable, where converting to a dynamic library is one of many options (none of which load the static library in its original form). – ShadowRanger Aug 12 '19 at 23:50
One option I've used to do this is to execute a python script from within a C\C++ application. The Boost.Python implementation is what I used to accomplish this. I statically linked the library file to the C app and ran the python script from the app. The methods in the C app that call into your static C library can be exposed to the python script.
Although the above is a solution...another way to do it is to build a pipe client in python, then connect to a C app (that is running a pipe server) that is statically linked to your library.

- 91
- 1
- 9