Export function from DLL in Visual Studio 2010 usi

2019-06-17 08:51发布

问题:

I inherited a huge C++ multi-project solution with many dynamic libraries but without any

__declspec(dllexport)

I learned that one does not necessarily have to insert any dllexport (would be much work) but that one can use a .def file in addition to corresponding .dll instead.

In order to try that I built a "DLL Hello World" project from here, removed the dllexport from the header and...failed desperately. In the words of already cited page, my key question is how to

"[..] use the .def file when building the DLL."

My .def file is (I try the code only with the Add method):

LIBRARY   MathFuncsDll
EXPORTS 
?Add@MyMathFuncs@MathFuncs@@SANNN@Z

How do I use it when building the DLL in Visual Studio 2010 in order to export the Add method?

回答1:

After having passed half a day in front of this problem, I just found the solution: it is described here.

To resume the process of symbol export with .def files in VS2010 using my own words:

  1. Tell VS2010 to compile a dynamic library (.dll). This is done in the Property Page of the library's project.
  2. Craft a module definition file (.def) by using mangled (decorated) names (at least when Your language is C++). If You make use of dllexport You can display already exported symbols of Your .dll as described here. If You haven't anything exported yet, see this post.
  3. Add the .def to the library definition in its Property Page.
  4. Compile
  5. Verify the correctness of Your work, for example with Dependency Walker by opening the dependent file, e.g. .exe. You should see the just compiled library in a dependency tree below the dependent file. There should be no errors or warnings, e.g. no red colour.

If You have further questions concerning .def files, look out for the terminus "Module definition file".