开发者

Benefits of exporting a class from a dll vs. static library

I have a C++ class I'm writing now that will be used all over a project I'm working on. I have the option to put it in a static library, or export the class from a dll. W开发者_StackOverflow中文版hat are the benefits/penalties for each approach. The only one I can think of is compiled code size which I don't really care about. Thanks!


Advantages of a DLL:

  • You can have multiple different exe's that access this functionality, so you will have a smaller project size overall.
  • You can dynamically update your component without replacing the whole exe. If you do this though be careful that the interface remains the same.
  • Sometimes like in the case of LGPL you are forced into using a DLL.
  • You could have some components as C#, Python or other languages that tie into your DLL.
  • You can build programs that consume your DLL that work with different versions of the DLL. For example you could check if a function exists in a certain operating system DLL and only call it if it exists, and otherwise do some other processing.

Advantages of Static library:

  • You cannot have dll verisoning problems that way
  • Less to distribute, you aren't forced into a full installer if you only have a small application.
  • You don't have to worry about anyone else tying into your code that would have been accessible if it was a DLL.
  • Easier to develop a static library as you don't need to worry about exports and imports.
  • Memory management is easier.


One of the most significant and often unnoted features of dynamic libraries on Windows is that DLLs have their own heap. This can be an advantage or a disadvantage depending on your point of view but you need to be aware of it. For example, a global variable in a DLL will be shared among all the processes attaching to that library which can be a useful form of de facto interprocess communication or the source of an obscure run time error.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜