The difference between static and shared libraries?

Static and shared libraries What is the difference?

I use E c lipse, and there are several types of projects, including the "static library" and "shared libraries"? A man advantage it than another person?


#1st Floor

For static library, the linker extracts the code from the library and used to build the final executable file at compile / build the application. Final executable file does not depend on the library at run time

For shared libraries, the compiler / linker will check your link with the name exists in the library when building applications, but not their code into an application. At runtime, shared library must be available.

C programming language itself has no concept of a static library or shared library - which is a completely realize the function.

Personally, I prefer to use a static library, because it makes the software distribution easier. However, this is the last point of view on a large number of (symbolic) bleeding.


#2nd Floor

The biggest advantage of shared libraries is that both use the library process how much memory is loaded only one copy of the code. For static library, each process will get its own copy of the code. This can lead to a lot of wasted memory.

OTOH, the advantages of a static library that bundles everything into your application. Therefore, you do not have to worry about the client will have the correct libraries (and version) on their systems.


#3rd floor

simplify:

  • Statically linked: a large executable file
  • Dynamic Link: a small executable file, together with one or more library files (on Windows .dll files on Linux is .so, on macOS is .dylib)

#4th floor

Static library as part of the application is compiled, and shared library is not. When you distribute applications that depend on shared libraries, these libraries for example. Dll needs to be installed on MS Windows.

Static libraries advantage is that the user does not need to run the application dependencies - for example, they do not have to upgrade their DLL. The disadvantage is that your application is large, because you comes with all required libraries.

In addition to shared libraries results in smaller applications, but also enables users to use their own, it might be a better version of the library, without having to rely on the part of the application


#5th Floor

.So files are shared libraries (or Windows .dll or OS X .dylib). All code associated with the library in this document, and the programs that use it will refer to it running. Program that uses shared libraries it uses only reference the code in a shared library.

Static library is .a (or in the Windows .lib) file. All code with libraries in this file, and link directly into the program at compile time. Use static library to obtain a copy of the program code from the use of static libraries, and as part of the program. [Windows .lib files are also used to reference the .dll files, but their role is the same as the first one. ]

Each method has advantages and disadvantages:

  • Reducing the shared library program uses the library for each repeated code amount, so that the binary smaller. It also allows you to replace shared objects with functionally equivalent shared objects, but may increase the performance benefits without having to recompile programs that use shared objects. However, the implementation of the shared library function will bring a small amount of additional costs and the cost of running the load, because all symbol libraries need to be connected to the things they use. In addition, shared libraries can be loaded at runtime into the application, which is a common mechanism for binary plug-in system.

  • Static library adds to the overall size of the binary file, but this means that you need to carry a copy of the library is being used. Since the code is connected at compile time, so there is no cost to load any additional runtime. Code there.

Personally, I prefer a shared library, but need to make sure the binaries are not using a lot of static library dependencies outside may be difficult to meet, such as a specific version of a specific version or Boost C ++ libraries of C ++ standard library.


#6th floor

Static library is like a bookstore, a shared library like ... library. Use the former, you can get your book / copy function, you can take home. For the latter, you and everyone else go to the library to use the same book / functions. Therefore, anyone who wants to use (shared) libraries must be aware of its location, because you have to "go get 'the book / functions. With a static library, you can have this book / function, and save it in your own family / program, once you have it, you do not care about when and where.


#7th floor

In addition to all the other answers, there is no mention of a thing decoupling:

Let me talk about the real world I have been treated in production code:

A lot of software, consisting of more than 300 projects (using Visual Studio), mainly as a static library built, eventually linked to a large executable file, leading to the following questions:

- Links a very long time. You may end up spending more than 15 minutes of links to time, for example 10 seconds compile time - some of the tools have such a large executable files, such as memory checker must check the code. You may fall into is regarded as a fool limits.

More troublesome is the decoupling of software: In this practical example, the header file for each project can be accessed from any other project. As a result, a developer adds depend very easy. It only contains the title, because the link at the end will allow to find symbols. It is a terrible cycle of dependence and total chaos ended.

For shared libraries, it requires some extra work, because developers have to edit the project build system to add dependent libraries. I observed that the shared library code tend to provide a more concise code API.


Building # 8

-------------------------------------------------------------------------
|  +-  |    Shared(dynamic)       |   Static Library (Linkages)         |
-------------------------------------------------------------------------
|Pros: | less memory use          |   an executable, using own libraries|
|      |                          |     ,coming with the program,       |
|      |                          |   doesn't need to worry about its   |
|      |                          |   compilebility subject to libraries|
-------------------------------------------------------------------------
|Cons: | implementations of       |   bigger memory uses                |
|      | libraries may be altered |                                     |
|      | subject to OS  and its   |                                     |
|      | version, which may affect|                                     |
|      | the compilebility and    |                                     |
|      | runnability of the code  |                                     |
-------------------------------------------------------------------------
Original articles published 0 · won praise 1 · views 2507

Guess you like

Origin blog.csdn.net/asdfgh0077/article/details/103945671