Jump to content
  • Advertisement
Sign in to follow this  
spree

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

This topic is 3916 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!