6

I have a DLL that is written in C++ and I want to suppress the name mangling for a few exported methods. The methods are global and are not members of any class. Is there a way to achieve this?

BTW: I'm using VS2008.

Shog9
  • 156,901
  • 35
  • 231
  • 235
Ohad Horesh
  • 4,340
  • 6
  • 28
  • 45

3 Answers3

26

Surround the function definitions with extern "C" {}

extern "C" {
    void foo() {}
}

See http://www.parashift.com/c++-faq-lite/mixing-c-and-cpp.html

bradtgmurray
  • 13,683
  • 10
  • 38
  • 36
  • 1
    You need both the declaration and the definition to be extern "C"ed, right? – leander May 25 '09 at 20:18
  • 1
    @leander No, just the declaration is sufficient; as long as the declaration is included at the point of definition and the signatures match exactly, the "C"-ness will be applied to the definition automatically. You can put it on both and if, e.g., the parameters don't match exactly you'll get an error (C2733 in VS) instead of the wrong version being exported silently. – Tim Sylvester Oct 10 '17 at 22:07
11

You can avoid all manglings (C++, cdecl, stdcall,...) for exported functions using a .def file with an EXPORTS section. Just create a MyDll.def file and add it to your project:

LIBRARY "MyDLL"
EXPORTS
  Foo
  Bar

Actually, chances are the wizard already created a def file for you. You just have to fill in the EXPORTS section.

Serge Wautier
  • 21,494
  • 13
  • 69
  • 110
11

"bradtgmurray" is right, but for Visual C++ compilers, you need to explicitly export your function anyway. But using a .DEF file as proposed by "Serge - appTranslator" is the wrong way to do it.

What is the universal way to export symbols on Visual C++ ?

Using the declspec(dllexport/dllimport) instruction, which works for both C and C++ code, decorated or not (whereas, the .DEF is limited to C unless you want to decorate your code by hand).

So, the right way to export undecorated functions in Visual C++ is combining the export "C" idiom, as answered by "bradtgmurray", and the dllimport/dllexport keyword.

An example ?

As an example, I created on Visual C++ an empty DLL project, and wrote two functions, one dubbed CPP because it was decorated, and the other C because it wasn't. The code is:

// Exported header
#ifdef MY_DLL_EXPORTS
#define MY_DLL_API __declspec(dllexport)
#else
#define MY_DLL_API __declspec(dllimport)
#endif

// Decorated function export : ?myCppFunction@@YAHF@Z
MY_DLL_API int myCppFunction(short v) ;

// Undecorated function export : myCFunction
extern "C"
{
MY_DLL_API int myCFunction(short v) ;
} ;

I guess you already know, but for completeness' sake, the MY_DLL_API macro is to be defined in the DLL makefile (i.e. the VCPROJ), but not by DLL users.

The C++ code is easy to write, but for completeness' sake, I'll write it below:

// Decorated function code
MY_DLL_API int myCppFunction(short v)
{
   return 42 * v ;
}

extern "C"
{

// Undecorated function code
MY_DLL_API int myCFunction(short v)
{
   return 42 * v ;
}

} ;
Community
  • 1
  • 1
paercebal
  • 81,378
  • 38
  • 130
  • 159
  • I am creating a class within an explicit Dll. Calling constructor for this class generates error "Unresolved external symbol...". I guess this is due to name mangling. Please write how to avoid this problem. DEF file or extern "C" is not helping here. – null Mar 20 '13 at 09:05
  • @ajay : I cannot help you in a comment. What you could do was to use Visual Studio to generate a DLL with example symbols to see how it is done. If you're still stuck, you should post (a limited version of) your code on StackOverflow (or CodeReview?), including both the DLL code (the header, the source), the EXE code linking to it (the source), and the error message. It is possible your error comes from the wrong constructor overload call: Is there an error if you try to call the same constructor from inside the DLL? If yes, then you need to declare/define the right constructor. – paercebal Mar 21 '13 at 10:36