How to manually mangle names in Visual C++?

Viewed 1947

If I have a function in a .c like

void foo(int c, char v);

...in my .obj, this becomes a symbol named

_foo

...as per C name mangling rules. If I have a similar function in a .cpp file, this becomes something else entirely, as per the compiler-specific name mangling rules. msvc 12 will give us this:

?foo@@YAXHD@Z

If I have that function foo in the .cpp file and I want it to use C name mangling rules (assuming I can do without overloading), we can declare it as

extern "C" void foo(int c, char v);

...in which case, we're back to good old

_foo

...in the .obj symbol table.

My question is, is it possible to go the other way around? If I wanted to simulate C++ name mangling with a C function, this would be easy with gcc because gcc's name mangling rules only make use of identifier-friendly characters, thus the mangled name of foo becomes _ZN3fooEic, and we could easily write

void ZN3fooEic(int c, char v);

Back in Microsoft-compiler-land, I obviously can't create a function whose name is a completely invalid identifier called

void ?foo@@YAXHD@Z(int c, char v);

...but I'd still like that function to show up with that symbol name in the .obj symbol table.

Any ideas? I've looked through Visual C++'s supported pragmas, and I don't see anything useful.

3 Answers
Related