Click to See Complete Forum and Search --> : Name mangling


kzseattle
February 12th, 2003, 03:07 PM
I am trying to export some functions from a DLL. Typically, I include C linkage in the function declaration so that it looks like follows:

extern "C" void someFunc(void *);

As you know, this prevents name mangling of the function by the C++ compiler. Then, I list the function in the DEF file as an exported function.

The problem is that I am trying to export a function that is also a friend of class. However, doing the following does not compile:

extern "C" friend void someFunc(void*);

But if I exclude the C linkage by not including extern "C" in the function declaration, then I must list the function with its mangled name in the DEF file. So, in the DEF file, it would look something like this:

?someFunc@@YGHPAXK@Z

This is compiler dependent and not portable.

So my question is: how do I export a function from a DLL that is also a friend of a class without mangling its name? If this is not possible, and I must export it with its mangled name, how do I find out the mangled name (so I can list it in the DEF file)?

PaulWendt
February 12th, 2003, 04:30 PM
You don't put the friend keyword on the function's declaration as
far as I know. You put the friend in the class's definition. That
is the reason you're getting compiler errors.


class A
{
public:
friend void externalFunction();
};

extern "C" void externalFunction()


Right?

Graham
February 13th, 2003, 04:25 AM
Yeah. You'd have to make sure that the extern "C" declaration is the first one that the compiler sees, though.

// externFn.h

extern "C" void externalFunction();

//end



// SomeClass.h

#include "externFn.h"

class SomeClass
{
friend void externalFunction();
// ...
};