# Pros and cons of using dlls [closed]

I wonder about create every game engine module (render, input, sound, etc...) into dlls (renderer.dll, input.dll, etc...). What are pros and cons in your opinion of divide game into set of dlls ? I see only need to write game module in separate dll, to have posibility to create mods.

• Pros and Cons compared to a static library or to no library at all? Oct 7 '12 at 17:50

Pros

• You can build DLLs separately.
• It could be faster to re-build one DLL

Cons

• Calling code from DLL is slower
• It would be slower to re-build hole project with all DLLs
• Function names are visible. It is easier to reverse code that uses dynamic DLLs

I do not see sense to have render.dll and input.dll But some RareUsedJoystick.DLL could be made as loadable module to release memory for those who do not use that RareUsedJoystick

• Have you any sources on this? From my understanding and experience of compiling and linking I would say nothing but the last point is true. Oct 7 '12 at 16:55
• In huge projects linking is much faster when you use dlls. Also note that it is harder to debug complex types in a dll. Oct 7 '12 at 16:59
• Static linking may inline functions. It is not possible to inline DLL function because DLL may change in future. It is also a reason why DLL linking is faster. I was saying it would be slower to compile all DLLs separately then compile all code in single EXE
– Max
Oct 7 '12 at 17:14
• The compiler/linker can optimize better when linking statically. But that doesn't mean that calling functions is generally slower. Oct 7 '12 at 17:56
• function in dynamic link library is called slower. Because your code calls function by index in array. It is extra 1-3 operations but many functions are just getter/setters that it self are 1-3 ops. I case of static linking and inlining it could 1 CPU op. Without inlining static lib could use registers to pass function args. In case of DLL. it uses stack to pass args. It takes function address by index in array. There is a lot of extra ops that static linking removes.
– Max
Oct 7 '12 at 20:25

Putting code into seperate Windows DLLs sounds promosing in theory, but in practice you should be aware of certain limitations.

1. You have to declare all your classes and global functions you want to use from outside as "exportable" using

__declspec( dllexport ) class Foo {};


when building the library. Clients of your DLL must "see" the include like this:

__declspec( dllimport ) class Foo {};


That is why you see a makro infront of class declarations in DLL headers.

2. Resources like FILE* pointers can not be shared under all circumstances (different CRTs) between DLLs. That means if you open the file in one DLL, you cannot write to it from the client app or another DLL, nor close it.

http://msdn.microsoft.com/en-us/library/ms235460%28VS.80%29.aspx

3. Memory management: You cannot allocate memory in the DLL and free it in another DLL or in client application under all circumstances (different CRTs).

4. Exporting template classes can be a real pain, especially if they use static member variables.

If you just want to reuse your code without recompiling, there is another option, static libraries (.lib). It is precompiled code (as in the DLL) but it is linked into your application and has no such limitations as the ones mentioned above, plus Link Time Code Generation still works with the library code.

Update: As Sean pointed out in the comments, point 2 and 3 can work when using the same CRT version, as the MSDN link in 2 also shows. However, my advice is to design the DLL without relying on it at all, since there are so many cases where it won't work, like using 2 DLLs with different CRT's or compiled with different Visual Studio versions.

• The memory and FILE* claims (2 and 3) are not quite true. Those problems occur when the DLL uses a different CRT version. If built with the app itself, this is not a problem. Oct 7 '12 at 19:04
• @Sean right, in theory. In practice there are many cases where it won't work (see the MSDN link), like DLLs from different VS versions, linking with 2 DLLs with different CRTs, so it's safer to design the DLL to not to rely on it. It's an important limitation to keep in mind and should be listed here. However, I put an update in my answer reflecting your comment, thanks Sean Oct 7 '12 at 19:40
• I'd argue it's not "just theory." Sure, if you're designing DLLs as standalone components, it's a problem. The original question is about just breaking up a single app into DLLs, which implies that the DLLs are built together at the same time with the same compiler. This is actually somewhat common in game engines for a variety of reasons (e.g., loading different optimized versions of code for different CPU instruction set variants), and in these cases, it is in practice totally safe to assume those problems won't happen. :) Oct 7 '12 at 20:40
• It should be also noted that this only applies if you use MSVC. GCC (MinGW) exports everything per default. Oct 7 '12 at 20:58
• @Mr.Beast Would that be export or import? I'm using MinGW myself. But got rather confused about the default settings for DLLs. Oct 8 '12 at 10:07

Most differences are negligible. DLL's are more flexible but make the building process more complex. I would only use DLL's if:

• Multiple applications need to access the same functionality. For example if you have separate binaries for a editor and the game itself.
• You want yourself or other people be able to replace certain components. For example in Civ IV modders could ship replacement for the game's DLL's to fundamentally change parts of the gameplay.
• You want to load them at runtime to allow to extend the application by third party plugins.

Another advantage is that it can help you structure your code correctly. Any non-logical dependencies between modules will stand out more and it will be more difficult to use such hacks.