静态库和动态库之间的差异

为什么要使用库? (Why using libraries?)

Typically C functions/C++ classes and methods which can be shared by more than one application are broken out of the application’s source code, compiled, and bundled into a library. The C standard libraries and C++ STL are examples of shared components that can be linked with your code. The benefit is that every object file need not be stated when linking because the developer can reference the library collective. This simplifies the multiple-use and sharing of software components between applications. It also allows application vendors a way to simply release an API to interface with an application.

通常,可以由多个应用程序共享的C函数/ C ++类和方法会从应用程序的源代码中分解出来,进行编译并捆绑到一个库中。 C标准库和C ++ STL是可以与您的代码链接的共享组件的示例。 好处是链接时不需要声明每个目标文件,因为开发人员可以引用库集合。 这简化了应用程序之间软件组件的多次使用和共享。 它还允许应用程序供应商一种方法来简单地释放API以与应用程序接口。

它们如何工作? (How do they work?)

Image for post

动态库: (Dynamic library:)

Dynamic libraries include the address of the beginning of the function that we are trying to use in the executable file.

动态库包括我们试图在可执行文件中使用的函数开始的地址。

静态库: (Static library:)

During the linking phase, the linker includes in the executable the modules corresponding to the functions and library classes that have been used in the application. As a result, such modules become part of the executable, just like any other function or class that would have been written in the body of the application.

在链接阶段,链接器在可执行文件中包括与应用程序中已使用的功能和库类相对应的模块。 结果,这些模块将成为可执行文件的一部分,就像将在应用程序主体中编写的任何其他函数或类一样。

如何创建它们? (Linux) (How to create them? (Linux))

静态库 (Static library)

Suppose we have 2 source files and one header: exa1.c, exa2.c, exa.h. Now what we must do is use a command that its definition tells us: “create, modify and extract files”. This command is “ar” and together with its -rc flags, we can create our library:

假设我们有2个源文件和1个标头:exa1.c,exa2.c,exa.h。 现在,我们必须做的是使用一个其定义告诉我们的命令:“创建,修改和提取文件”。 这个命令是“ ar”,并且连同它的-rc标志一起,我们可以创建我们的库:

ar -rc libexa.a exa1.o exa2.o

ar -rc libexa.a exa1.o exa2.o

Now we must take into account 3 important things and they are:1. The -r flag is used to replace or overwrite files that are repeated (that have already been invoked).2. the -c flag is to create the library if it does not exist.3. To obtain our object-files we must compile them in this way: gcc -c exa1.c exa2.c

现在我们必须考虑3个重要的事情,它们是:1。 -r标志用于替换或覆盖重复的文件(已被调用)2。 -c标志用于创建不存在的库3。 要获取目标文件,我们必须以这种方式进行编译:gcc -c exa1.c exa2.c

Finally, to finish creating our library, all we have to do is index our library, which will do is list each symbol defined by a member of a file that is a relocatable object file:

最后,完成创建我们的库,我们要做的就是索引我们的库,这将列出由文件的成员定义的每个符号,该文件是可重定位的目标文件:

ranlib libexa.a

ranlib libexa.a

动态库 (Dynamic library)

To create a dynamic library you must have 2 important things:

要创建动态库,您必须具有以下两个重要事项:

  • a header with the name of the prototypes of the functions of the “C” files.

    标有“ C”文件功能原型名称的标头。
  • We must have the “.o” files.

    我们必须有“ .o”文件。
  1. First, we must compile our “c” files, using the command:

    首先,我们必须使用以下命令编译“ c”文件:

gcc -fPIC -c *.c

gcc -fPIC -c * .c

With gcc we will compile our files and with the flag “-c” we compile the source files “c” and with the flag -fPIC it generates “code independent of the position” and with “* .c” we are telling it to take all files ending in “.c”. This will generate our “.o” files

使用gcc,我们将编译文件,使用标志“ -c”,我们将编译源文件“ c”,使用标志-fPIC,它将生成“与位置无关的代码”,并使用“ * .c”来告诉它提取所有以“ .c”结尾的文件。 这将生成我们的“ .o”文件

2. Now we need to use the command:

2.现在我们需要使用以下命令:

gcc -shared -o libnamedynamiclibrary.so *.o

gcc -shared -o libnamedynamiclibrary.so * .o

With this command, we will create our dynamic library. using the -share flag to tell the compiler to produce a shared file that can be linked with other objects to form an executable and the -o flag with which we will indicate the name of our library, and finally, we use “* .o” to indicate that it takes all our files ending in “.o”

使用此命令,我们将创建动态库。 使用-share标志告诉编译器生成一个可以与其他对象链接以形成可执行文件的共享文件,使用-o标志指示我们的库名,最后使用“ * .o” ”表示将接收所有以“ .o”结尾的文件

如何使用它们? (How to use them?)

静态库: (Static library:)

Now to be able to use our library we must compile it in such a way that the program can understand where it will find the function of our files:

现在,要能够使用我们的库,我们必须以一种程序可以理解它将在哪里找到文件功能的方式来编译它:

gcc main.c -L. -lexa -o exe

gcc main.c -L。 -lexa -o exe

Considering this command:- The -L flag will make it search for the library files in the directory.- He “ . ” represents the current library.- the -l flag helps us “link” the library. Note that the prefix “lib” and the extension “.a” are removed. In such a way that “lexe” would remain.- With the -o flag we will be saying to name our executable file as “exe”.

考虑以下命令:--L标志将使其在目录中搜索库文件。-He“。 ”表示当前库。--l标志帮助我们“链接”该库。 请注意,前缀“ lib”和扩展名“ .a”已删除。 -保留-lexe的方式。-使用-o标志,我们将把可执行文件命名为“ exe”。

动态库: (Dynamic library:)

For dynamic libraries, it is necessary to define the location so that the program knows where to look. we use:+

对于动态库,有必要定义位置,以便程序知道在哪里查找。 我们使用:+

export LD_LIBRARY_PATH=. :$LD_LIBRARY_PATH

导出LD_LIBRARY_PATH =。 :$ LD_LIBRARY_PATH

We have to define the environment variable LD_LIBRARY_PATH, in which we place all the directories where there are dynamic libraries of interest. now we verify that the library is already stored, using “ldd (library name)” and finally we can use “nm” to verify the symbols of the dynamic library with the “-D” flag to indicate that it is dynamic.

我们必须定义环境变量LD_LIBRARY_PATH,在其中将所有目录放置在感兴趣的动态库中。 现在,我们使用“ ldd(库名称)”验证该库是否已存储,最后我们可以使用“ nm”通过“ -D”标志来表明该动态库的符号是动态的。

静态库和动态库之间有什么区别? (What are the differences between static and dynamic libraries?)

动态库: (Dynamic libraries:)

These are dynamically linked by simply including the address of the library. Dynamic linking links the libraries at run time. Thus, all functions are in a special place in the memory space, and each program can access them, without having multiple copies of them.

通过简单地包含库地址可以动态链接这些库。 动态链接在运行时链接库。 因此,所有功能都位于内存空间中的特殊位置,并且每个程序都可以访问它们,而无需多个副本。

静态库: (Static libraries:)

In static libraries, the libraries are added to the executable code so that the executable file can be loaded on any machine and run. Using the command file, we come to know that by default gcc follows dynamic binding and can be linked statically using the –static flag with gcc.

在静态库中,将库添加到可执行代码中,以便可执行文件可以在任何计算机上加载并运行。 使用命令文件,我们知道默认情况下gcc遵循动态绑定,并且可以使用–static标志与gcc进行静态链接。

它们各自的优缺点是什么? (What are the advantages and drawbacks of each of them?)

静态库: (Static library:)

+ faster.+ without dependency.- increased memory usage.

+更快。+无依赖关系。-增加内存使用量。

动态库: (Dynamic library:)

+ little memory usage.+ If a .so has not been loaded yet, the executable’s remaining undefined symbols should be resolved at startup.- There is the possibility of breaking executables without relinking.

+很少的内存使用。+如果尚未加载.so,则应在启动时解决可执行文件剩余的未定义符号。-有可能在不重新链接的情况下破坏可执行文件。

翻译自: https://medium.com/analytics-vidhya/differences-between-static-and-dynamic-libraries-17dd5e41da96

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值