First off, what is the difference between static linking (.lib file) and a dynamic link (.dll file) |
For starters, it has nothing to do with .lib/.dll. A .lib could be static or dynamic (though dlls are always dynamic)
Dynamic linking makes it so your program loads the library and uses it at runtime. The library is loaded from a separate file (the dll file -- although you would tell VS to link to the .lib file in your project)
It might help to think of dlls as mini-exes. They contained compiled code just like your exe does.
Static linking effectively builds the dll file into your exe so you don't need to load it from a seperate file at runtime.
Advantages for dynamic libs:
- smaller exe size
- multiple programs can share the same dll file(s)
Advantages for static libs:
- fewer dll dependecies
is it OK to statically link, even for commercial programs? |
It depends on the license. See the license of whatever library you're using.
When I build a library it usually makes a lib file, can I move that lib file anywhere? |
I typically put them in a designated "lib" folder somewhere, and tell VS where that folder is. That way I can easily link to them later.
Is that file the only file I need to use that library in any project? |
You'll also need the headers.
And if you're dynamically linking, you'll also need the dll(s).
If linking statically is a bad idea, |
It's not. I tend to prefer it for easier distribution. Especially in C++. Dynamic linking doesn't work as well in C++ as it does in C.
how do I debug while using a dll? |
There are often debug versions of dlls. Although you don't need to debug code in the dll (not unless it's a dll you're writing) so it's kind of a moot point.
Even if the dll isn't a debug build you can step through your code just fine, no matter how the lib is linked.
Will code-hinting work with a dll? |
you mean like intellisense stuff? It usually gets that from the headers, so yeah it will work fine. Remember you need the headers regardless of how you're linking.