linux # .a & .so

2018-07-25  本文已影响16人  FlyingPenguin

Linux Library Types:

There are two Linux C/C++ library types which can be created:

  • Static libraries (.a):
    Library of object code which is linked with, and becomes part of the application.
  • Dynamically linked shared object libraries (.so):
    There is only one form of this library but it can be used in two ways.
    Dynamically linked at run time. The libraries must be available during compile/link phase. The shared objects are not included into the executable component but are tied to the execution.
    Dynamically loaded/unloaded and linked during execution (i.e. browser plug-in) using the dynamic linking loader system functions.

A .a file is a static library, while a .so file is a shared object dynamic library similar to a DLL on Windows.
A .a can included as part of a program during the compilation & .so can imported, while a program loads.

.a是静态库。
.so是动态库。

When you link against the *.a, the code from the library is included in the executable itself and the executable can be run without requiring the *.a file to be present.
When you link against the *.so, that is not the case and the *.so file must be present at runtime.

.a的方式,.a中的内容是包含在二进制文件中的,所以二进制文件运行的时候不需要*.a file。

Static Libraries: (.a)

As a follow on, a .a file is an "ar" archive.
Not unlike a tar archive, it stores .o or object files, allowing them to be pulled out of the archive, and linked into a program, among other things. You could use ar to store other files if you wanted.

Create library "libctest.a"
ar -cvq libctest.a ctest1.o ctest2.o
get a listing of the members of an ar file

You can get a listing of the members of an ar file with the -t parameter, for instance:

linux下,在ffmpeg目录下

 ar -t libavformat/libavformat.a

运行结果:

[root@localhost libavformat]# ar -t libavformat.a
3dostr.o
...
mov.o
...
yuv4mpegdec.o
yuv4mpegenc.o
Linking with the library
cc -o executable-name prog.c libctest.a
cc -o executable-name prog.c -L/path/to/library-directory -lctest

Dynamically Linked "Shared Object" Libraries: (.so)

A .so file is a "shared object" file, and has a lot more information available to the linker so that members can be linked in to a loading program as rapidly as possible.

References:

https://stackoverflow.com/questions/12293530/what-is-the-difference-between-so-and-a-files
https://unix.stackexchange.com/questions/13192/what-is-the-difference-between-a-and-so-file
http://www.yolinux.com/TUTORIALS/LibraryArchives-StaticAndDynamic.html

上一篇 下一篇

猜你喜欢

热点阅读