Where Is My .lib?
Previously in another article I tried to remember how to use shared libraries on Windows. Now I tried to build and ran into a minor obstacle.
There in the build output directory is the
.dll, but not
.lib. Without it I can't use the library I just built.
As it turned out, I must export the symbols explicitly for the
.lib to get generated. This behavior is the opposite of default GCC visibility attribute behavior. Exporting is simple, simply prefix the class or function I want to make available with
EXPORT, defined like this:
#define EXPORT __declspec(dllexport)
This is shorter to type, and allows
EXPORT to be defined differently for another build target.
Separately, you may want to run some code when a
.dll is loaded or unloaded. On Windows this is done by defining your own
DllMain function, as documented on MSDN.
Finally, one of my reasons for using Visual Studio is for debugging support. But I had to ignore warning C4251 when I build, because I use STL classes.
To disable specific warning,
alt-F7 to bring up project properties, under C/C++ → Advanced → Disable Specific Warnings, add
4521 to entry field.
Alternatively, understand that DLL interface likes passing buffers so we want to avoid using STL types.