静态链接图书馆的利弊是什么?

时间:2022-09-18 12:30:48

I want to release an application I developed as a hobby both for Linux and Windows. This application depends on boost (and possibly other libraries). The norm for this kind of application (a chess engine) is to provide only an executable file and possibly some helper files.

我想发布一个我开发的应用程序,作为Linux和Windows的业余爱好。此应用程序依赖于boost(以及可能的其他库)。这种应用程序(国际象棋引擎)的规范是仅提供可执行文件和可能的一些帮助文件。

I tough it would be a good idea to statically link the libraries so the executable would not have any dependencies. So the end user can just put the executable in a directory and start using it.

我认为静态链接库是个好主意,因此可执行文件不会有任何依赖。因此,最终用户可以将可执行文件放在目录中并开始使用它。

However, while doing some research online I found some negative comments about statically linking libraries, some even arguing that an application with statically linked libraries would be hardly portable, meaning that it would only run on my system of highly similar systems.

然而,在线进行一些研究时,我发现了一些关于静态链接库的负面评论,有些人甚至认为具有静态链接库的应用程序几乎不可移植,这意味着它只能在我的高度相似系统系统上运行。

So what are the pros and cons of statically linking library?

那么静态链接库的优缺点是什么?

I already know that the executable will be bigger. But I can't see why it would make my application less portable.

我已经知道可执行文件会更大。但我不明白为什么它会使我的应用程序不那么便携。

4 个解决方案

#1


5  

Pros:
No dependencies.

优点:没有依赖性。

Cons:
Higher memory usage, as the OS can no longer use a shared copy of the library.
If the library needs to be updated, your application needs to be rebuilt. This is doubly important for libraries that then have security fixes.

缺点:内存使用率较高,因为操作系统无法再使用库的共享副本。如果需要更新库,则需要重新构建应用程序。这对于具有安全修复程序的库来说非常重要。

Of course, a bigger issue for portability is the lack of source code distribution.

当然,可移植性的一个更大问题是缺乏源代码分发。

#2


2  

Let's say the static library "A" you include has a dependency on function "B". If this dependency can't be fulfilled by the target system, then your program won't run.

假设您包含的静态库“A”依赖于函数“B”。如果目标系统无法满足此依赖关系,则您的程序将无法运行。

But if you're using dynamic linking, the user could maybe install another version of library "A" that uses function "C" instead of "B", so it can run successfully.

但是如果你使用动态链接,用户可以安装另一个版本的库“A”,它使用函数“C”而不是“B”,因此它可以成功运行。

#3


0  

If you link the libraries statically, unless you add the smarts to also check the user's system for the libraries you've linked, you're locking your application to use those versions of the libraries until you update your executable. Security holes happen, and updates happen. (For a chess engine there may not be too much issue, but who knows.)

如果您静态链接库,除非您添加智能也检查用户系统中已链接的库,您将锁定应用程序以使用这些版本的库,直到您更新可执行文件。发生安全漏洞,并发生更新。 (对于国际象棋引擎,可能没有太多问题,但谁知道。)

#4


0  

With dynamically linked libraries, if the library say X, you have linked with is not available at the user system, your code crashes ungracefully leaving the end user wondering.
Whereas, in the case of static libraries everything is fused into the executable, so a condition like above mayn't happen, the executable however will be very bulky.

使用动态链接的库,如果库说X,那么你在用户系统中没有链接,你的代码会崩溃,让最终用户不知所措。然而,在静态库的情况下,所有内容都融合到可执行文件中,因此上面的条件可能不会发生,但是可执行文件会非常笨重。

The above problem in dynamically linked libraries can however, be eliminated by dynamic loading.

但是,动态链接库中的上述问题可以通过动态加载来消除。

#1


5  

Pros:
No dependencies.

优点:没有依赖性。

Cons:
Higher memory usage, as the OS can no longer use a shared copy of the library.
If the library needs to be updated, your application needs to be rebuilt. This is doubly important for libraries that then have security fixes.

缺点:内存使用率较高,因为操作系统无法再使用库的共享副本。如果需要更新库,则需要重新构建应用程序。这对于具有安全修复程序的库来说非常重要。

Of course, a bigger issue for portability is the lack of source code distribution.

当然,可移植性的一个更大问题是缺乏源代码分发。

#2


2  

Let's say the static library "A" you include has a dependency on function "B". If this dependency can't be fulfilled by the target system, then your program won't run.

假设您包含的静态库“A”依赖于函数“B”。如果目标系统无法满足此依赖关系,则您的程序将无法运行。

But if you're using dynamic linking, the user could maybe install another version of library "A" that uses function "C" instead of "B", so it can run successfully.

但是如果你使用动态链接,用户可以安装另一个版本的库“A”,它使用函数“C”而不是“B”,因此它可以成功运行。

#3


0  

If you link the libraries statically, unless you add the smarts to also check the user's system for the libraries you've linked, you're locking your application to use those versions of the libraries until you update your executable. Security holes happen, and updates happen. (For a chess engine there may not be too much issue, but who knows.)

如果您静态链接库,除非您添加智能也检查用户系统中已链接的库,您将锁定应用程序以使用这些版本的库,直到您更新可执行文件。发生安全漏洞,并发生更新。 (对于国际象棋引擎,可能没有太多问题,但谁知道。)

#4


0  

With dynamically linked libraries, if the library say X, you have linked with is not available at the user system, your code crashes ungracefully leaving the end user wondering.
Whereas, in the case of static libraries everything is fused into the executable, so a condition like above mayn't happen, the executable however will be very bulky.

使用动态链接的库,如果库说X,那么你在用户系统中没有链接,你的代码会崩溃,让最终用户不知所措。然而,在静态库的情况下,所有内容都融合到可执行文件中,因此上面的条件可能不会发生,但是可执行文件会非常笨重。

The above problem in dynamically linked libraries can however, be eliminated by dynamic loading.

但是,动态链接库中的上述问题可以通过动态加载来消除。