.def file vs. __declspec(dllexport) macro

2.4k views Asked by At

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!

3

There are 3 answers

0
user3409863 On

Def files are a better solution. Help avoid a lot of deployment and backward compatibility problems. Also from the point of view of code maintenance is easier, since you see all the exports in a single place. The "mangling name" problem is easily avoided by adding the extern "C" in the function declaration. Another consideration is that declspec export export the function with the name mangled, a third party consumer, will need to use the exact same compiler you used, and/or.get lib files that are tighly coupled.to your linker.

0
Ed Greaves On

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
Deqing On

.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.