Static library (.lib) to Python project

人走茶凉 提交于 2019-12-22 04:42:10

问题


is it possible to import modules from .lib library to Python program (as simple as .dll)?


回答1:


In theory, yes; in practice, probably not -- and certainly not as simply as a DLL. Static libraries are essentially just collections of object files, and need a full linker to correctly resolve all relocation references they may contain. It might be possible to take your static library and simply link its contents to form a shared library, but that would require that the static library had been built as position independent code (PIC), which is not guaranteed. In theory there's no reason the work a full linker would do to link the library couldn't be done at runtime, but in practice there's no off-the-shelf code for doing so. Your best real option is probably to track down the source or a shared version of the library.




回答2:


Unfortunately, no. Dynamic Link Libraries are required for runtime loading.




回答3:


Do you have access to the source code? Or at least a header file? If you do, then you could either create a shared library or a Python extension which links to the library. Since you mentioned DLLs, I'll assume you're working on Windows. This tutorial may be useful.




回答4:


Do you have a static library or do you have a .lib file and are assuming that it is a static library? On Windows, a .lib library can be an import library or a static library. An import library is created alongside the dll of the same name (eg kernel32.dll and kernel32.lib). It is used at link time to populate the import address table of the executable. A static library contains code that will be copied into the executable at link time.

If you have access to a compiler, another option may be to create an extension module that makes use of the static library. For more details see the Python docs



来源:https://stackoverflow.com/questions/3668373/static-library-lib-to-python-project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!