Linux 中的等效导入库

Equivalent of import libraries in Linux

在 Windows C++ 中,当您想 link 针对 DLL 时,您必须提供一个导入库。但是在 GNU 构建系统中,当你想要 link 反对 .so 文件时,这些文件相当于你不需要的 dll。为什么是这样?是否有 Windows 导入库的等价物。

注意:我不会谈论在 Windows 中使用 GNU C++ 的情况,您也必须使用导入库。分割线在 Windows C++ 和 Linux C++ 之间。

Windows 和 Linux 中的链接模型不同。阅读 Levine 的书 Linkers and loaders (on Linux, every public symbol of a library is exported, unless you play visibility tricks;在 Windows 上,情况并非如此,导出的符号需要显式显示)。

C++11 standard (read n3337) 别提动态链接了。这是一个实现细节。

未来C++20 could have modules.

Linux中没有“导入库”。

更多详情,请注意name mangling is different. Read also Program Library Howto, Drepper's How to Write Shared Libraries

在 Linux、plugins are loaded (and handled differently than on Windows) by the dynamic loader. See ld-linux(8), dlopen(3), dlsym(3), elf(5)

检查,Linux,ELF files (object files, libraries, executables) with objdump(1) and readelf(1) and nm(1)

另见 C++ dlopen mini howto. Read also about the Visibility function attribute. See also this 问题。

.so files which are the equivalent of dll

一个Linuxshared object (ELF .so file) is not exactly equivalent to a Windows DLL。阅读上面给出的参考资料。

我还建议阅读 Operating Systems: Three Easy Pieces and the old Advanced Linux Programming (both are freely downloadable). Later read syscalls(2) 和那里引用的页面。

还要注意 Linux 是 free software,因此您可以下载并研究其大部分组件的源代码。

PS。 Linux和Windows真的不一样。不要指望在 Linux 中找到每个 Windows 功能的精确等价物。 以全新的眼光看待 Linux。 利用 Linux 由免费软件构成的优势,并考虑研究源代码,例如libc.so 上方的 kernel, binutils, GNU libc or musl-libc (both providing some ld-linux.so and libc.so, so a C standard library), GCC or Clang (both providing a C++ standard library

为了补充 Basile 的答案,您可能偶尔需要在 Linux 上导入库来模拟 delay loading 共享库(这很有用,例如,如果您的应用很少需要这个库,而您又不需要想在上面浪费资源)。

这样的模拟导入库将由一堆包装器组成,这些包装器在内部调用 dlopendlsym,然后转到共享库中的实现。它们可以通过项目特定脚本或通用工具手动实现 Implib.so.