DLL Module-Definition Files |
|
Definition File Fundamentals |
We mentioned that a dll must provide a means of importing its functions and making them available to client applications. We learned above how to help the compiler create the import library by preceding at least one function with the _declspec(dllexport) modifier. Microsoft Windows (I don't know if this technique is available on Linux although I know Linux also uses DLLs) allows another technique. Instead of preceding your functions with a modifier, you can instead add another file called the Module-Definition file. A definition file is a text file that has the extension def. It must contain at least two sections. The first section is made of one line. It starts with the LIBRARY word followed by the name of the DLL. It is important that the name you specify be the same name as the DLL that will be made available to other applications. The second section starts with the EXPORTS word and contains a list of the functions that will be exported. In the following exercise, we will create a DLL that can be used to calculate the moment of inertia using the following illustrations and formulas:
The moment of inertia is taught in technical high school in Mechanical Engineering or in the first two years of university in Engineering Science. For the following exercises, you don't need to know anything about the moment of inertia. We will simply apply the above formulas.
|
|
Usage of a Definition File DLL |
Although we mentioned that you can create a definition file to have an import library, you don't have to use that definition file, although you can. The technique we used above allows the compiler to know that you need an import library. This means that there are at least two different ways you can make use of a definition file (as we stated already, the most difficult aspect of a DLL is based on decisions, not how to create it). Once again, you can use the dll and lib files in the client applications of your DLL, as we did already. If you don't want to use the definition file, you must provide a mechanism for a client application to locate the function that is included in your DLL. This time also, there are different ways you can do this. When a client application wants to use a function, it can declare the function in the file that will call the function. The declaration is done as a normal C++ function declaration. The function must be listed (exactly) as it appears in the DLL. This is why you should (strongly) always provide documentation for your DLL: other people or companies shouldn't spend time predicting or guessing what your DLL is used for or what it contains. In the following example, a function called Number() is called from main(). The function is only declared but it is not defined because this was already taken care of in a DLL:
Another technique consists of including the header file that contains the function definitions from the DLL. This means that, besides the dll and the lib files, if you distribute your library, you must also distribute the header file (provided you are not distributing the def file, although you can distribute both). |
Practical Learning: Using a Module Definition DLL |
|
|
||
Previous | Copyright © 2004-2006 FunctionX, Inc. | Next |
|