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. What 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!
+8
A:
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.
Brian R. Bondy
2009-11-24 20:05:34
good reason, I didn't think of versioning
Steve
2009-11-24 20:07:26
One way to avoid DLL version problems is to add the version number to the name of the DLL file (e.g. as a suffix, like `mydll300.dll`).
Loadmaster
2009-11-24 20:09:11
BTW, thanks for continuing to refine your answer
Steve
2009-11-24 20:13:17
No problem, I think I'm mostly done for now :)
Brian R. Bondy
2009-11-24 20:15:49
A:
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.
William Bell
2009-11-24 20:28:01
That is not 100% correct. A global var in a DLL is instanced per process. You can, with pragmas/linker tricks create system globals but that is not the default. It was as William says in Win3.1 but that changed in Win95/NT.
jmucchiello
2009-11-24 20:37:37