Sign in to follow this  
spree

Exporting DLL symbols using .def file and Name mangling (Windows)

Recommended Posts

Hi Everybody, I"m using MS VS2003, and tryig to create a basic dll + lib file without a wizard. I need help with creating a correct .def file for my exports. In the code below I wish to export the constructor, destructor and the printX methods only. The test.h file:
#ifndef _TEST_
#define _TEST_
#define _TEST() test a;
class test
{
public:
test(int);
~test();
private:
int x;
public:
void printX();
};
#endif

-------------------------------------------------------------- The test.cpp file: -------------------------------------------------------------
#include "test.h"

#include <stdio.h>

test::test(int _x):x(_x)

{

printf("test x(%d)\n",x);

}

test::~test()

{

printf("~test x(%d)\n",x);

}

void test:rintX()

{

printf("%d\n",x);

}

------------------------------------------------------------------- And I need help with creating my .def file (test.def) ------------------------------------------------------------------ LIBRARY "test.dll" EXPORTS what goes here? Thanks!!!

Share this post


Link to post
Share on other sites
I don't think C++ offers a way to explicitly export class members. I can't see how this would be useful either, as no interfaces exist to bind to a type library at run-time (unlike function resolution, which is available via GetProcAddress and its kin).

Simply declare the class as a DLL-export and the linker will take care of the rest:

class __declspec(dllexport) test {
...


Now the class's public members will appear in the binary's export table (mangled) and all the appropriate symbols will be produced for the library definition.

Share this post


Link to post
Share on other sites
I found something on the net http://www.kegel.com/mangle.html#operators
And this document suggests that my constructor and destructors are to be mangled like so:


LIBRARY "test.dll"
EXPORTS
??0test@@QAE@XZ
??1test@@QAE@XZ



This .def file links without errors, I shall test this dll now and hopefuly it wokrs :)

Share this post


Link to post
Share on other sites
Uh oh, you do not go around writing down mangled names in your DEF files. It is inherently unportable, as mangling is *compiler specific*. That mangling scheme will compile with nothing other than your own compiler. Congratulations.

Since C++ mangles symbol names, for exporting with def files, you need to turn off symbol mangling. C has no name mangling, so wrap your code around in extern "C" { ... }

Share this post


Link to post
Share on other sites
The correct way of working with classes in DLLs is to provide a C interface which instantiates the class and returns a pointer to it. That way, you don't need to export constructors/destructors.

What you basically do is this:

extern "C"
{
CTest *CreateTest();
void FreeTest(CTest *p);
}


When you need a CTest instance, you call CreateTest() in your code, and voila, you have an instance of said class.

Toolmaker

Share this post


Link to post
Share on other sites
Its OK by me that it will only work with my compiler.
If it was not a specific requirment I'd just go with the wizard build of the dll.

Another thing, I saw some def files which export the name of the functions unmangled, but it did not work for me though...

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this