开发者

Where should I be using a static library in C++

What are the use cases of using static libraries in C++? I have seen that people create DLLs instead开发者_如何学编程 or some that use static libraries only. Whats your recommendation?


I'm a big fan of static libraries pretty much everywhere. The one big thing that DLLs get you that static libs cannot do is the ability to dynamically load and unload library functionality. So if your application is going to support some sort of hot swapping plugins, you need to use dynamic libs. Otherwise you can probably use static libs.

Static libs open the door to a lot of optimizations that you can't do with dynamic libs because they are performed at link-time. In the microsoft world Link Time Code Generation (LTCG) give you the ability to do whole program optimization and dead code stripping through not only your application, but also your libraries (in gcc this is called Link Time Optimization [LTO])

Additionally static libs tend to make your program easier to distribute because you aren't forced to pass around a lot of library files, and you can completely avoid DLL-hell if you ever were to version your library.


You should use shared libraries (DLL) if you have a significant functionality that needs to be shared between applications; AND this functionality may be improved independant of all the application and updates shipped seprately.

The 'AND' part is the hardest to fulfill: usually you ship your application with any new functionality added and never update the library without updating the application at the same time (I am not saying that never happens) but usually the two ship in lockstep.

Otherwise it is easier to just build normal libs and ship the application.

An Example of a good (I use the term loosely for example purposes) is DirectX. When a new version of DirectX is shipped (and the interface has not changed) you just need to update the DLL and all apllications that use DirectX get the benifit of the new version of the library. In reality it is not quite that simple but you get the idea.


In general, although there are always exceptions to the rule, I would say:

Advantages of DLLs

  • Less physical memory usage when running multiple instances of an application. (Copy on write optimisation of memory usage.)
  • Faster link times.
  • Smaller executables.
  • Better modularity.

Advantages of static libraries

  • Less virtual memory usage (and probably less physical memory usage) when running a single instance of an application.
  • Performance. Approximately 10% (more or less) improvement over DLLs, depending on your application.
  • Reliability. You tested your application against a specific version (or specific versions) of a library. An upgrade to a DLL could potentially break your application.


There is the advantage of not having to recompile your entire program if you make a change to a dynamically linked library. @Chris makes a good point about dll-hell but if it s a minor bug fix that doesn't affect the API, this can save you the recompilation.

There is a SO post that talks about Windows not being able to apply updates to your program if you statically link their libraries (link to come). Although i think you are more talking about statically linking your own modules.


Use static version of your libraries where you can. Use dynamic libraries where you need to (license, availability or plugin system).


I use static libraries to implement UML's "package" concept. All modules belonging to a package gets put into their own subdirectory, and I create an IDE subproject or makefile for that directory which builds a static library *.a file. Modern IDEs make it possible to work with your top-level package along with sub-packages within the same "workspace".

If a package (or a group of packages) can be deployed separately from the main executable, then I compile it into a shared library (*.so or *.dll) instead and consider it a "component" in UML jargon.


Well a Static DLL would be for holding huge libraries and also for using Multi-Os coode as i like to call it so it's able to be ran on Linux , Windows ...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜