Use Your DLL (Windows)

All DLLs must be in a directory listed in the PATH environment variable.

To link with a DLL, include the import library (.lib) file for the DLL on the linker command line. The linker uses the import library to resolve references made from your application to the DLL.

Write the source files that are to access your DLL as if the functions and/or variables are to be statically linked at compile time. Then when you link the program, you must inform the linker that some function and/or variable references are to a DLL and will be resolved at run time. To communicate this information to the linker:

  1. Use the _Import or __declspec(dllimport) keywords with a function name or external variable to declare the function or variable is to be imported from a DLL. The function must be defined in the same compilation unit in which the _Export keyword is used.

    You must mark data imports by _Import or __declspec(dllimport) and it is recommended that you also mark functions which are being imported as well. You will get slightly better performance from the generated code.

    Note: The /qautoimport switch (default is /qnoautoimport) removes the need to mark data imports in the DLL client by assuming that ALL external data references will be resolved at runtime. This "import everything" assumption may be what you want in certain situations. For example, the use of this switch would allow the same set of objects to be used for both static and dynamic linking. However, while /qautoimported may be easier for the programmer, marking function and data imports in a DLL client generates more efficient code.
  2. Use the LIB file created when you built the DLL.
  3. When you link an executable module, the linker uses this LIB import library to resolve external references to the DLL. You must always access the DLL by means of an import library.

    If you do not have a library file, you can use ILIB to first generate a module definition file. Verify and edit the entries in the module definition file, then use ILIB with it to generate a library file.

    If you invoke the linker directly, give the name of the import library where you normally specify library names. For example:

         ILINK /DLL mymain.obj finaldll.lib

    If you invoke the linker through the icc command, you must put the name of the import library in the compiler invocation string. For example:

       icc /Ge- mymain.obj finaldll.lib

    The import libraries for the IBM C and C++ Compilers runtime DLLs have been supplied with the compiler.



Create a DLL - An Overview