I'd like to mangle and demangle C++ function names in a Python program. Is there anything like that available? I searched for hours now, perhaps I'm lucky here...
dllexport of a C++ function will expose the function with C++ name mangling. To disable name mangling either use a . def file (EXPORTS keyword) or declare the function as extern “C”.
Python. In Python, mangling is used for class attributes that one does not want subclasses to use which are designated as such by giving them a name with two or more leading underscores and no more than one trailing underscore.
Since C is a programming language that does not support name function overloading, it does no name mangling.
To prevent the C++ compiler from mangling the name of a function, you can apply the extern "C" linkage specifier to the declaration or declarations, as shown in the following example: extern "C" { int f1(int); int f2(int); int f3(int); };
If you want to demangle names, eg. for display, then you can create a pipe that runs c++filt.
def demangle(names):
args = ['c++filt']
args.extend(names)
pipe = subprocess.Popen(args, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
stdout, _ = pipe.communicate()
demangled = stdout.split("\n")
# Each line ends with a newline, so the final entry of the split output
# will always be ''.
assert len(demangled) == len(names)+1
return demangled[:-1]
print demangle(['_ZNSt15basic_stringbufIcSt11char_traitsIcESaIcEE17_M_stringbuf_initESt13_Ios_Openmode',
'_ZNSt15basic_stringbufIcSt11char_traitsIcESaIcEE6setbufEPci'])
You can specify arguments to c++filt if you need to use a specific demangling method.
Mangling a name is much harder, and probably can't be done without knowing the definitions of the types involved.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With