.lib 和 .a 文件有什么区别?
我正在尝试静态编译一些东西,并且我正在尝试了解所有这些依赖项是什么。我知道 .dll 文件用于最终输出所需的动态链接依赖项,但是 .a 和 .lib 文件是什么以及何时需要它们?
I'm trying to statically compile something and I'm trying to get a handle on what all these dependencies are. I know that .dll files are for dynamically linked dependencies that will be required by the final output, but what are .a and .lib files and when do you need each of those?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
.a 是代码存档:已编译但未链接。您可以在程序的最后链接步骤中静态链接它。
.lib 可以与 .a 相同,也可以是一个神奇的所谓“导入库”:一个薄占位符,使您在运行时需要 .dll。
.a is an archive of code: compiled but not linked. You would link statically with it during your program's final link step.
.lib can be either the same as .a, or a magical so-called "import library": a thin placeholder which causes you to require a .dll at runtime.
在 Unix 系统上,您有
.a
文件。这些是目标文件的简单档案 (.o
)。在 Windows 上,有
.lib
文件,它们是完全相同的东西,但用于 Windows 而不是 Unix。另一个微妙之处是,为了将某些代码链接到 DLL(在 Windows 上),您必须链接到包含调用 DLL 的简单包装器的
.lib
文件。在 Unix 系统上,传统上不需要这样的包装器(链接器足够智能,可以动态生成它们)。On Unix systems you have the
.a
files. These are simple archives of object files (.o
).On Windows, there are
.lib
files, which are quite the same thing, but for Windows instead of Unix.An additional subtlety is that in order to link some code against a DLL (on Windows), you have to link against a
.lib
file which contains simple wrappers which invoke the DLL. On Unix system, traditionally, there is no need for such wrappers (the linker is smart enough to generate them on the fly).通常,.a 用于 Linux 下的静态库,而 .lib 用于相同但在 Windows 上的静态库。但当然这只是一个约定。
Usually, .a is for static libraries under Linux whereas .lib are for the same but on Windows. But of course it is just a convention.
我在这里还没有看到提到的一个令人惊讶的事实是,至少在某些时候,.a 文件和 .lib 文件实际上是相同的二进制格式。虽然我在 mingw 网站上找不到任何说明,但我注意到,当尝试让 MS Visual C++ 的 64 位编译器 cl.exe 链接到使用 mingw-w64 g++ 编译器生成的 .dll 文件时,它很高兴 接受了命令行
当我将相应的 .dll 文件的副本放入当前目录中时, ,并且生成的 .exe 文件正确运行。 (它确实发出警告“命令行警告 D9024:无法识别的源文件类型 'path\to\lib\gmp-6.0.0\lib\libgmp.dll.a',假设为目标文件”。)
进一步的证据是 Linux
file
命令报告了我尝试过的每个扩展名(.lib 或 .a)的多个文件的“当前 ar 存档”。Something I don't see mentioned here yet is the surprising fact that, at least some of the time, .a files and .lib files are actually the same binary format. Although I couldn't find anything saying as much on the mingw website, I noticed that when trying to get MS Visual C++'s 64-bit compiler cl.exe to link in a .dll file produced using the mingw-w64 g++ compiler, it happily accepted the command line
and the resulting .exe file ran correctly as soon as I put a copy of the corresponding .dll file in the current directory. (It did mumble the warning "Command line warning D9024 : unrecognized source file type 'path\to\lib\gmp-6.0.0\lib\libgmp.dll.a', object file assumed".)
Further evidence is that the Linux
file
command reported "current ar archive" for several files of each extension (.lib or .a) I tried.