ChatGPT解决这个技术问题 Extra ChatGPT

When to use dynamic vs. static libraries

When creating a class library in C++, you can choose between dynamic (.dll, .so) and static (.lib, .a) libraries. What is the difference between them and when is it appropriate to use which?

It should be noted that there is also something which is called "Import Library" check stackoverflow.com/questions/3573475/…

O
Orion Adrian

Static libraries increase the size of the code in your binary. They're always loaded and whatever version of the code you compiled with is the version of the code that will run.

Dynamic libraries are stored and versioned separately. It's possible for a version of the dynamic library to be loaded that wasn't the original one that shipped with your code if the update is considered binary compatible with the original version.

Additionally dynamic libraries aren't necessarily loaded -- they're usually loaded when first called -- and can be shared among components that use the same library (multiple data loads, one code load).

Dynamic libraries were considered to be the better approach most of the time, but originally they had a major flaw (google DLL hell), which has all but been eliminated by more recent Windows OSes (Windows XP in particular).


On Windows/Mac (no package manager) there is really no good reason to use dynamic libraries over static. Since Windows DLLs are not relocatable, code sharing often does not work (and usually each app ships and uses its own versions of the library anyways). The only real benefit is that it is easier to update the library.
on the mac, I use a lot of dynamic libraries. for example, mac os x has sqlite3 embed. i created a program that has a sqlite3 database feature for performance storing. however, because it is rarely used dynamic linking saves on compile time, makes testing easier/faster however, if I were to build a release version, I think I would always use static library just in case of compatibility issues
@Zifre: relocatable = can be loaded at different virtual address. DLL certainly supports this.
@dma_k: Windows DLLs can be loaded at different addresses, but only because the linker copies all the code and changes the address numbers. With shared objects, all the address references are relative, so multiple processes can share the same memory for the shared object. In other words, on Windows, A 1 MB DLL used by 3 programs = 3 MB. On Linux, A MB SO used by 3 programs = 1 MB.
Both Windows and Linux has the concept of load-timte relocation of shared libraries eli.thegreenplace.net/2011/08/25/… The biggest thing that allowed Position Independent Code wasn't something special to Linux, rather RIP-relative addressing added with the x64 instruction set; both Windows and Linux can make use of RIP relative addressing do reduce the number of fixups when relocating libraries.
b
bk1e

Others have adequately explained what a static library is, but I'd like to point out some of the caveats of using static libraries, at least on Windows:

Singletons: If something needs to be global/static and unique, be very careful about putting it in a static library. If multiple DLLs are linked against that static library they will each get their own copy of the singleton. However, if your application is a single EXE with no custom DLLs, this may not be a problem.

Unreferenced code removal: When you link against a static library, only the parts of the static library that are referenced by your DLL/EXE will get linked into your DLL/EXE. For example, if mylib.lib contains a.obj and b.obj and your DLL/EXE only references functions or variables from a.obj, the entirety of b.obj will get discarded by the linker. If b.obj contains global/static objects, their constructors and destructors will not get executed. If those constructors/destructors have side effects, you may be disappointed by their absence. Likewise, if the static library contains special entrypoints you may need to take care that they are actually included. An example of this in embedded programming (okay, not Windows) would be an interrupt handler that is marked as being at a specific address. You also need to mark the interrupt handler as an entrypoint to make sure it doesn't get discarded. Another consequence of this is that a static library may contain object files that are completely unusable due to unresolved references, but it won't cause a linker error until you reference a function or variable from those object files. This may happen long after the library is written.

Debug symbols: You may want a separate PDB for each static library, or you may want the debug symbols to be placed in the object files so that they get rolled into the PDB for the DLL/EXE. The Visual C++ documentation explains the necessary options.

RTTI: You may end up with multiple type_info objects for the same class if you link a single static library into multiple DLLs. If your program assumes that type_info is "singleton" data and uses &typeid() or type_info::before(), you may get undesirable and surprising results.


As for the point about singletons, don't forget that a DLL might be loaded multiple times (same version or mulitple versions) and there's still no singleton guarantee.
Additional point about unreferenced code removal: Calls made to DLLs also require an actual call to force the loading of the referenced DLL. Adding it as a reference, but then not including any call that references it will still get you the same result as having a static libary that doesn't call anything. The only difference is what actually ships. In both cases the static constructors and destructors don't fire.
@bk1e That shouldn't happen. the .a will always contain all the symbols it was built with. When it's statically linked into your application, yes only those symbols that are used will be linked in.
I don't yet understand most of your post but +1 for specific things for us Windows scrubs.
A
Anirudh Ramanathan

A lib is a unit of code that is bundled within your application executable.

A dll is a standalone unit of executable code. It is loaded in the process only when a call is made into that code. A dll can be used by multiple applications and loaded in multiple processes, while still having only one copy of the code on the hard drive.

Dll pros: can be used to reuse/share code between several products; load in the process memory on demand and can be unloaded when not needed; can be upgraded independently of the rest of the program.

Dll cons: performance impact of the dll loading and code rebasing; versioning problems ("dll hell")

Lib pros: no performance impact as code is always loaded in the process and is not rebased; no versioning problems.

Lib cons: executable/process "bloat" - all the code is in your executable and is loaded upon process start; no reuse/sharing - each product has its own copy of the code.


Rebasing can also be done at build time using rebase.exe or by passing the /BASE option to link.exe. Whether this is effective depends on whether there are any unexpected address space conflicts at runtime.
t
tcb

C++ programs are built in two phases

Compilation - produces object code (.obj) Linking - produces executable code (.exe or .dll)

Static library (.lib) is just a bundle of .obj files and therefore isn't a complete program. It hasn't undergone the second (linking) phase of building a program. Dlls, on the other hand, are like exe's and therefore are complete programs.

If you build a static library, it isn't linked yet and therefore consumers of your static library will have to use the same compiler that you used (if you used g++, they will have to use g++).

If instead you built a dll (and built it correctly), you have built a complete program that all consumers can use, no matter which compiler they are using. There are several restrictions though, on exporting from a dll, if cross compiler compatibility is desired.


This is news to me. What restrictions are there with cross compilers when using DLLs? Having the programmer build without needing the same toolchain seems like a huge plus for DLLs
This answer is informative. Adding minor caveat: consumers of your static library will have to use the same compiler that you used if the static library uses C++ library, such as #include <iostream>.
one cannot consume a c++ dll unless the same compiler is used (because there is not standard c++ abi, symbols are mangled in different ways). Both the dll and the client module must use the same compiler and same build settings
Please laborate on how you acheive cross compiler compatibility by building dynamic library. I recalled you'd get hiccups even across gcc and clang. Though they try to follow the same ABI standard. If a real cross compiler shared library is created, I'd say only the one with C signatures.
r
rburhum

Besides the technical implications of static vs dynamic libraries (static files bundle everything in one big binary vs dynamic libraries that allow code sharing among several different executables), there are the legal implications.

For example, if you are using LGPL licensed code and you link statically against a LGPL library (and thus create one big binary), your code automatically becomes Open Sourced (free as in freedom) LGPL code. If you link against a shared objects, then you only need to LGPL the improvements / bug fixes that you make to the LGPL library itself.

This becomes a far more important issue if you are deciding how to compile you mobile applications for example (in Android you have a choice of static vs dynamic, in iOS you do not - it is always static).


V
Vijay

Creating a static library

$$:~/static [32]> cat foo.c
#include<stdio.h>
void foo()
{
printf("\nhello world\n");
}
$$:~/static [33]> cat foo.h
#ifndef _H_FOO_H
#define _H_FOO_H

void foo();

#endif
$$:~/static [34]> cat foo2.c
#include<stdio.h>
void foo2()
{
printf("\nworld\n");
}
$$:~/static [35]> cat foo2.h
#ifndef _H_FOO2_H
#define _H_FOO2_H

void foo2();

#endif
$$:~/static [36]> cat hello.c
#include<foo.h>
#include<foo2.h>
void main()
{
foo();
foo2();
}
$$:~/static [37]> cat makefile
hello: hello.o libtest.a
        cc -o hello hello.o -L. -ltest
hello.o: hello.c
        cc -c hello.c -I`pwd`
libtest.a:foo.o foo2.o
        ar cr libtest.a foo.o foo2.o
foo.o:foo.c
        cc -c foo.c
foo2.o:foo.c
        cc -c foo2.c
clean:
        rm -f foo.o foo2.o libtest.a hello.o

$$:~/static [38]>

creating a dynamic library

$$:~/dynamic [44]> cat foo.c
#include<stdio.h>
void foo()
{
printf("\nhello world\n");
}
$$:~/dynamic [45]> cat foo.h
#ifndef _H_FOO_H
#define _H_FOO_H

void foo();

#endif
$$:~/dynamic [46]> cat foo2.c
#include<stdio.h>
void foo2()
{
printf("\nworld\n");
}
$$:~/dynamic [47]> cat foo2.h
#ifndef _H_FOO2_H
#define _H_FOO2_H

void foo2();

#endif
$$:~/dynamic [48]> cat hello.c
#include<foo.h>
#include<foo2.h>
void main()
{
foo();
foo2();
}
$$:~/dynamic [49]> cat makefile
hello:hello.o libtest.sl
        cc -o hello hello.o -L`pwd` -ltest
hello.o:
        cc -c -b hello.c -I`pwd`
libtest.sl:foo.o foo2.o
        cc -G -b -o libtest.sl foo.o foo2.o
foo.o:foo.c
        cc -c -b foo.c
foo2.o:foo.c
        cc -c -b foo2.c
clean:
        rm -f libtest.sl foo.o foo

2.o hello.o
$$:~/dynamic [50]>

J
Jordan Parmer

A static library gets compiled into the client. A .lib is used at compile time and the contents of the library become part of the consuming executable.

A dynamic library is loaded at runtime and not compiled into the client executable. Dynamic libraries are more flexible as multiple client executables can load a DLL and utilize its functionality. This also keeps the overall size and maintainability of your client code to a minimum.


s
spotcatbug

A static library must be linked into the final executable; it becomes part of the executable and follows it wherever it goes. A dynamic library is loaded every time the executable is executed and remains separate from the executable as a DLL file.

You would use a DLL when you want to be able to change the functionality provided by the library without having to re-link the executable (just replace the DLL file, without having to replace the executable file).

You would use a static library whenever you don't have a reason to use a dynamic library.


You might also use a DLL when multiple other applications use the same functionality - this can reduce footprint.
Also, extending your initial concept, "plug-in" architecture where you want to allow added/unknown functionality later without having to rebuild or re-release can only be done with dynamic libraries.
C
Corey Trager

You should think carefully about changes over time, versioning, stability, compatibility, etc.

If there are two apps that use the shared code, do you want to force those apps to change together, in case they need to be compatible with each other? Then use the dll. All the exe's will be using the same code.

Or do you want to isolate them from each other, so that you can change one and be confident you haven't broken the other. Then use the static lib.

DLL hell is when you probably SHOULD HAVE used a static lib, but you used a dll instead, and not all the exes are comaptible with it.


V
Void

Ulrich Drepper's paper on "How to Write Shared Libraries" is also good resource that details how best to take advantage of shared libraries, or what he refers to as "Dynamic Shared Objects" (DSOs). It focuses more on shared libraries in the ELF binary format, but some discussions are suitable for Windows DLLs as well.


R
Rob Wells

For an excellent discussion of this topic have a read of this article from Sun.

It goes into all the benefits including being able to insert interposing libraries. More detail on interposing can be found in this article here.


p
pfranza

Really the trade off you are making (in a large project) is in initial load time, the libraries are going to get linked at one time or another, the decision that has to be made is will the link take long enough that the compiler needs to bite the bullet and do it up front, or can the dynamic linker do it at load time.


D
Dima

If your library is going to be shared among several executables, it often makes sense to make it dynamic to reduce the size of the executables. Otherwise, definitely make it static.

There are several disadvantages of using a dll. There is additional overhead for loading and unloading it. There is also an additional dependency. If you change the dll to make it incompatible with your executalbes, they will stop working. On the other hand, if you change a static library, your compiled executables using the old version will not be affected.


S
Seb Rose

If the library is static, then at link time the code is linked in with your executable. This makes your executable larger (than if you went the dynamic route).

If the library is dynamic then at link time references to the required methods are built in to your executable. This means that you have to ship your executable and the dynamic library. You also ought to consider whether shared access to the code in the library is safe, preferred load address among other stuff.

If you can live with the static library, go with the static library.


g
gast128

We use a lot of DLL's (> 100) in our project. These DLL's have dependencies on each other and therefore we chose the setup of dynamic linking. However it has the following disadvantages:

slow startup (> 10 seconds)

DLL's had to be versioned, since windows loads modules on uniqueness of names. Own written components would otherwise get the wrong version of the DLL (i.e. the one already loaded instead of its own distributed set)

optimizer can only optimize within DLL boundaries. For example the optimizer tries to place frequently used data and code next to each other, but this will not work across DLL boundaries

Maybe a better setup was to make everything a static library (and therefore you just have one executable). This works only if no code duplication takes place. A test seems to support this assumption, but i couldn't find an official MSDN quote. So for example make 1 exe with:

exe uses shared_lib1, shared_lib2

shared_lib1 use shared_lib2

shared_lib2

The code and variables of shared_lib2 should be present in the final merged executable only once. Can anyone support this question?


Aren't you meant to use some pre-compiler directives in some way to avoid code duplication?
Afaiac precompiling only works on a per module (exe / dll /lib) base. Precompiling is primarly meant to speed up compilation though it also prevents multiple inclusions within a compilation unit. However include guards are the better way to achieve this effect.
T
Terence Simpson

Static libraries are archives that contain the object code for the library, when linked into an application that code is compiled into the executable. Shared libraries are different in that they aren't compiled into the executable. Instead the dynamic linker searches some directories looking for the library(s) it needs, then loads that into memory. More then one executable can use the same shared library at the same time, thus reducing memory usage and executable size. However, there are then more files to distribute with the executable. You need to make sure that the library is installed onto the uses system somewhere where the linker can find it, static linking eliminates this problem but results in a larger executable file.


R
Robert Gould

If your working on embedded projects or specialized platforms static libraries are the only way to go, also many times they are less of a hassle to compile into your application. Also having projects and makefile that include everything makes life happier.


t
the_mandrill

I'd give a general rule of thumb that if you have a large codebase, all built on top of lower level libraries (eg a Utils or Gui framework), which you want to partition into more manageable libraries then make them static libraries. Dynamic libraries don't really buy you anything and there are fewer surprises -- there will only be one instance of singletons for instance.

If you have a library that is entirely separate to the rest of the codebase (eg a third party library) then consider making it a dll. If the library is LGPL you may need to use a dll anyway due to the licensing conditions.


G
Gupta

Apart from all the points that have been mentioned by others, I use static libraries in a specific use-case to:

Not allow my end users to get access to some general-purpose libraries that I have developed in my code.

In other words, suppose that I have two libraries in my product, A and B. A uses B services and depends on it. But B is a general-purpose library including lots of helpful services that can be used separately. To avoid my end users to benefit from B directly (they should pay for its license!), I usually compile B as a static library and put it directly inside A. As a result, B services would be totally private to A and cannot be used by end users.