LIBs vs LoadLibrary()

Started by
2 comments, last by Andrew Russell 18 years, 10 months ago
What is better and faster? Using lib files or load functions using LoadLibrary()?
Advertisement
I think you're asking about static vs. dynamic linking.

Static linking with libraries is faster, and easier to code for. If you can get away with it, go for static linking.

However, for some things static linking just isn't safe. If you're using libraries or functions that you can't guarantee are supported on the target system, then static linkage isn't really good enough - the program will simply bomb out as soon as you try and run it on such a system. Also, dynamic linking enables you to do some other tricks, like choose your library implementation at run time.

The XP theme code I posted in your other thread loads them dynamically because these functions are not supported at all on any OS other than XP. If it had linked to the xptheme library statically, the program would not run at all on any other platform.
Or are you talking about import libraries vs. dynamic loading? In that case I would use the first one where possible (see post above). It's easier to code since the compiler takes care of the correct calling and you don't have to manage the function pointers for the entry points by yourself.
Using .lib files are safe and easy when you're using your own DLLs (although then you may want to use LoadLibrary), and is also pretty safe for common system DLLs (opengl32.lib and winmm.lib are two that come to mind from my own projects).

For fancy stuff that's only supported on some systems (like Sandman's example), you should use LoadLibrary.

In any case, their speed will not matter - your linking should not be happening in speed-critical sections and the actual calling of functions is the same speed.

This topic is closed to new replies.

Advertisement