Question

I am trying to build a dll written in C and which will be imported by other programs also written in C.

So all the function that the dll is exporting are defined in a .dll "without __declspec(dllexport)" (intentional). I have defined a .def file with just an Export section and name of the functions I want to export (unmangled names).

I am using vc71/vs2003 to build this and I am still getting mangled names (which I can see if I open the .lib in notepad). Also just for clarification, visual studio causes name mangling in C code as well (most resources I could find mentioned it being an issue with only C++).

How can I prevent this name mangling?

Further Information:

The mangled names are of the form 'functionName@integer' where integer represent the parameter size in bytes (and not ordinal). For example,

From .lib: _PrepareSeverResponse@8

Function declaration in .h: char* PrepareSeverResponse(unsigned int* size ,handshake* ws_handshake);

.def: EXPORTS PrepareSeverResponse

Calling Convention: __stdcall(/Gz)

Hope this makes it clearer.

Was it helpful?

Solution

Changing calling convention to cdecl worked for me. The extern "C" solution won't of much help to me as in my case the problem was with name mangling while compiling .C files where as if I understand it correctly the extern "C" solution is for suppressing name mangling when compiling cpp files.

OTHER TIPS

In order to prevent name mangling, you need to wrap your headers with extern C:

#ifdef __cplusplus
extern "C" {
#endif


//  Headers


#ifdef __cplusplus
}
#endif

This will force the symbols to their C-style (unmangled) names.

The name mangling you're seeing is unrelated to C++; it's Microsoft's calling convention name decoration, and applies to C code as well. You can alias these names back in your .DEF file:

EXPORTS
  PrepareServerResponse=_PrepareSeverResponse@8
  _PrepareSeverResponse@8

Note that when you link your library to another program, the host program will be looking for the decorated name. The main case where you might want an undecorated name is if you expect users to load your DLL using LoadLibrary and GetProcAddress.

Without seeing what you are characterizing as "name-mangling," I would guess that you have to set Visual Studio to Compile as C Code (/TC).

It's in Properties->C/C++->Advanced->Compile As. You can select this setting for your entire project since that's your intent.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top