7
votes

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
Can you show the prototype to your Add function? - cdarke
static double Add(double a, double b);, placed as public in class MyMathFuncs of namespace MathFuncs - Finnfalter
Are you exporting the class? See stackoverflow.com/questions/186232/… - cdarke
No, I don't but thank You for the hint anyway. - Finnfalter
Win32's GetWindowText() is an example of a "pure C interface" function exported from a DLL. You use extern "C", you don't use C++ objects at the interface, you don't throw C++ exceptions outside the function, etc. (The implementation can be in C++, but the interface is pure C.) But it seems you want to export a C++ class (static) method... - Mr.C64

1 Answers

12
votes

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