What's the difference between .lib and .a files?

天大地大妈咪最大 提交于 2019-11-28 03:40:14

.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.

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).

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

cl /EHsc /Ipath\to\include gmp_test.cpp path\to\lib\libgmp.dll.a

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.

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.

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