1
votes

Our team is considering to move from def file to __declspec(dllexport) macro because it has to use mangling name in the *.def files and, that is a tedious task to maintain those ugly looking names there. But we are still doubt is there any benefit from using *.def file over __declspec(dllexport) macro for function export.

Appreciate your suggestions!

2
Possible duplicate of Export function from a DLL - Use DEF file or dllexport? Some good information in both places, I think these should be merged.joshperry

2 Answers

2
votes

According to MSDN: https://msdn.microsoft.com/en-us/library/hyx1zcd3.aspx, there are four ways to export a definition, listed in recommended order:

  1. The __declspec(dllexport) keyword in the source code
  2. An EXPORTS statement in a .DEF file
  3. An /EXPORT specification in a LINK command
  4. A comment directive in the source code, of the form #pragma
  5. comment(linker, "/export:definition")

The advantages of .DEF files are as follows:

  1. You can export by ordinal

The disadvantages of .DEF files are that:

  1. You have an additional file to maintain
  2. You have to use the decorated function names

The advantage of exporting by ordinal is that you can reduce the size of the export table. However, then you have to use the ordinals instead of friendly names.

0
votes

.def files can export by ordinal values (@1, @2, ...) rather than by name. This can:

  1. increase symbol look up performance
  2. maintain the backward compatibility with the already released dlls, i.e. the ordinal numbers for the old apis are intact when adding a new api.

And it can avoid the function name from being decorated, which improves compiler compatibility (i.e. calling conventions difference of __stdcall and __cdecl). Here is an example of exporting undecorated name in dll.