Building Dynamic Libraries
Dynamic libraries are a little more complicated than static libraries in that when you build one, three files get placed in your filesystem. There is a .EXP file that contains a list of the function names that are "exported" by the library. Also there is an import library .LIB for the linker that contains references to those exports. Finally, there is the dynamically linked library .DLL itself.
In order that the function name be properly exported, the prototype of the function must be preceded by __declspec( dllexport ). Thus the C prototype:
int square( int );
becomes
__declspec( dllexport ) int square( int );
To build the dll, pass the /dll option to the linker and rename the output:
cl libsquare.c /link /dll /out:square.dll
Or in FORTRAN an ATTRIBUTES line needs to be added:
INTEGER FUNCTION SQUARE(X)
becomes
INTEGER FUNCTION SQUARE(X)
!DEC$ ATTRIBUTES DLLEXPORT :: SQUARE
FORTRAN building is easier because the /dll option is recognized by the compiler:
ifort square.f /dll
Using Dynamic Libraries
The import library (.LIB) for each dll needs to be linked with the other object files when the executable is built. From the command line this looks no different from building with a static library.
At runtime, the dynamic library must be available in the user's PATH. As mentioned above, programs that malfunction because of defective dlls can be fixed without recompilation just by replacing the bad dlls.