When building the DLL, the linker uses the DEF file to create an export (.exp) file and an import library (.lib) file. Leaving out these four lines might cause your DLL to compile or link incorrectly or cause an error when the client application links to the DLL. For example, when deriving a class using DECLARE_DYNAMIC, the macro expands to add a CRuntimeClass member variable to your class. These lines ensure that MFC variables that are used internally or that are added to your classes are exported (or imported) from your MFC extension DLL.
If you are building an extension DLL, and exporting using a DEF file, place the following code at the beginning and end of your header files that contain the exported classes: #undef AFX_DATA
If you place the decorated names produced by the Microsoft C++ compiler (MSVC) into a DEF file, applications that link to your DLL must also be built using the same version of MSVC so that the decorated names in the calling application match the exported names in the DLL's DEF file.Ī DLL built with Visual Studio 2015 can be consumed by applications built with Visual Studio 2017 or Visual Studio 2019.
Note that the decorated names produced by the compiler are compiler specific. If you need to place the decorated names in the DEF file, you can obtain them by using the DUMPBIN tool or by using the linker /MAP option. If you are exporting functions in a C++ file, you have to either place the decorated names in the DEF file or define your exported functions with standard C linkage by using extern "C". Repeat this step for each configuration and platform, or do it all at once by selecting Configuration = All Configurations, and Platform = All Platforms. Then go to Project > Properties > Linker > Input > Module Definition File and enter the name of the DEF file. For non-MFC DLLs, create the DEF file yourself and add it to your project. Add the names of the functions to be exported to this file. Insert you use the MFC DLL Wizard to create an MFC DLL, the wizard creates a skeleton DEF file for you and automatically adds it to your project. If you want to export functions by ordinal, see Exporting Functions from a DLL by Ordinal Rather Than by Name as well as this topic.įor example, a DLL that contains the code to implement a binary search tree might look like the following: LIBRARY BTREE When you specify ordinal values, they must be in the range 1 through N, where N is the number of functions exported by the DLL. You assign the function an ordinal value by following the function's name with an at sign and a number. The EXPORTS statement lists the names and, optionally, the ordinal values of the functions exported by the DLL. The linker places this name in the DLL's import library. The LIBRARY statement is followed by the name of the DLL. This statement identifies the DEF file as belonging to a DLL. The first statement in the file must be the LIBRARY statement. If you are not using the _declspec(dllexport) keyword to export the DLL's functions, the DLL requires a DEF file.Ī minimal DEF file must contain the following module-definition statements: A module-definition or DEF file (*.def) is a text file containing one or more module statements that describe various attributes of a DLL.