Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 555
Mathieu Pagé Avatar asked Mar 16 '10 15:03

Mathieu Pagé


2 Answers

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.

like image 127
Powerlord Avatar answered Jan 30 '23 12:01

Powerlord


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.

like image 44
Chris Lercher Avatar answered Jan 30 '23 12:01

Chris Lercher