简体   繁体   中英

Static library (.lib) to Python project

是否可以将.lib库中的模块导入到Python程序中(就像.dll一样简单)?

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.

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM