CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Name mangling

  1. #1
    Join Date
    Aug 2002
    Posts
    173

    Question 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)?

  2. #2
    Join Date
    May 2000
    Location
    Phoenix, AZ [USA]
    Posts
    1,347
    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?

  3. #3
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470
    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
  •  





Click Here to Expand Forum to Full Width

Featured