Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't prevent name mangling

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.

like image 629
Kartik Rustagi Avatar asked Dec 27 '22 11:12

Kartik Rustagi


2 Answers

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.

like image 126
Kartik Rustagi Avatar answered Jan 09 '23 05:01

Kartik Rustagi


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.

like image 20
Mysticial Avatar answered Jan 09 '23 04:01

Mysticial