views:

602

answers:

5

I've been reading a few gaming books. And they always prefer to create the engine as a static library over dynamic link. I am new to c++ so I am not highly knowledge when it comes to static libraries and dynamic link libraries. All I know is static libraries increase the size of your program, where DLL link libraries are loaded as you need them within your program.

[edit]

I've played games where it almost seemed they used DLL's to load in sound, lighting, and what not all individually. as the level was loading up. cause you don't necessarily need that when your at the game menu.

+13  A: 

Dynamic link libraries need to be position independent; this can cause performance inefficiencies on some processor architectures.

Static libraries can be optimized when included in your program, e.g., by stripping dead code. This can improve cache performance.

Doug Currie
Ok, you said they need to be positioned independent. Define position independent. cause how I look at it. Both DLL's and Static libraries would be both considered independent, in terms that they don't belong to the initial game. When you use static libraries. You are loading all of it's contents. So how exactly can you strip dead code from it ?
numerical25
The static library is linked with your executable at compile/link time; the linker knows the load address of the static library code, and can optimize for it. The linker also know which functions in the library are referenced, and can strip out the functions that are not referenced. The dynamic library is linked at run time, and may be shared by several processes, and therefore the address is not know a priori, nor can the library be moved. The dynamic library has entry points that may be used dynamically (duh) ;-), so no code can be eliminated from the memory footprint.
Doug Currie
By "position independent" I mean that the code must be able to run no matter where it is loaded in memory. This implies using relative jumps and pointer based data references (or PC relative const data references). Very smart loaders may do some work at load time to minimize the impact. However, static code can be compiled and linked to use known addresses for data and code.
Doug Currie
ok that makes sense, thanks.
numerical25
@Doug "very smart loaders may..." Actually, even smart, they will have a hard time doing that if the system allows the same dynamic library to be seen at *different* virtual addresses by two processes that are using it simultaneously. This is the choice in the ELF binary format for instance, I don't know much about the others.
Pascal Cuoq
@Pascal: Well, Windows runs primarily on x86, whose variable-width instructions make it literally impossible to deassemble a dll at load-time, so Windows does/can not change the binary either.
BlueRaja - Danny Pflughoeft
@BlueRaja If a DLL was guaranteed to be seen at the same address by all processes that use it, pre-linking it would not require disassembly. The old a.out format did that, but that was not practical (centralized worldwide authority for DLLs). I think that Mac OS X may have something like that too (but with the centralization only at the scale of the computer)
Pascal Cuoq
+5  A: 

By position independent, he means that since the game engine and DLL are completely separated, the DLL is stand-alone and cannot be interwoven into the game engine code, whereas statically linking a library allows the compiler to optimize using both your game engine code AND the library code.

For example, say there's a small function that the compiler thinks should be inlined (copied directly in place of a function call). Then with a static library, the compiler would be able to inline this code, since it knows what the code is (you're linking at compile-time). However, with a dynamic library, the compiler would be unable to inline that code, since it does not know what the code is (since it will be linking at run-time).

foxwoods
Inlining is not the reason dynamic libraries are slower. Most compilers+linkers do not inline functions across files. Actually, I program in a language where the compiler inlines across files, and the consequent lack of true separate compilation annoys me to no end.
Pascal Cuoq
True. I was just presenting inlining as an example of something that could be done _possibly_ with static libraries, as opposed to dynamic, to give a more concrete example.
foxwoods
Visual Studio has whole program optimization which will happily inline across files and probably across libraries too. I'd be surprised if gcc didn't have something similar (though most games will not be compiled in gcc).
Kylotan
+2  A: 

Another question covers the differences between static and dynamic libraries: http://stackoverflow.com/questions/140061/when-to-use-dynamic-vs-static-libraries

As for why they use static libraries, the extra speed may be worth it and you can avoid DLL hell (was a big problem in the past). It's also useful if you want to distribute your program and libraries together, ensuring the recipient has the correct dependencies, though there's nothing stopping you from distributing DLLs together with the executable.

Stephen Cross
A: 

Another often overlooked reason which deserves mention is that for many games you aren't going to be running lots of other stuff, and many libraries that are used for games aren't going to be used for the other things that you may be running at the same time as a game, so you don't have to worry about one of the major positives you get from using shared libraries, which is that only one copy of (most of) the library needs to loaded at one time while several things can make use of that one copy. When running a game you will probably only have one program that would want to use that library running anyway because you probably aren't going to be running many other programs (particularly other games or 3D programs) at the same time.

You also open up the possibility of global/link time optimization, which is much more difficult with shared libraries.

nategoose
+2  A: 

When developing games for a console, often dynamic linking isn't an option. If you want to use the engine for both console and PC development, it would be best to avoid dynamic linking.

caspin