Mathieu Pagé
Mathieu Pagé

Reputation: 11064

What are the pro and cons of statically linking a library?

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.

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.

Upvotes: 13

Views: 5300

Answers (4)

sud03r
sud03r

Reputation: 19769

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.

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

Upvotes: 0

Chris Lercher
Chris Lercher

Reputation: 37778

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.

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.

Upvotes: 1

John
John

Reputation: 16007

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

Upvotes: 0

Powerlord
Powerlord

Reputation: 88796

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.

Upvotes: 6

Related Questions