|
-
February 12th, 2003, 04:07 PM
#1
Name mangling
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)?
-
February 12th, 2003, 05:30 PM
#2
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.
Code:
class A
{
public:
friend void externalFunction();
};
extern "C" void externalFunction()
Right?
-
February 13th, 2003, 05:25 AM
#3
Yeah. You'd have to make sure that the extern "C" declaration is the first one that the compiler sees, though.
Code:
// externFn.h
extern "C" void externalFunction();
//end
// SomeClass.h
#include "externFn.h"
class SomeClass
{
friend void externalFunction();
// ...
};
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|