Questions tagged [name-mangling]

Name-mangling is a technique used by compilers (mainly C++ compilers) to encode information in strings that can be supported by linkers designed to handle C code.

Questions relating to the schemes used for name mangling, linking problems in the face of mangling, or the tools used to decode mangled names (like c++filt or dem) are appropriate for this tag.

In C++, name mangling is used to encode things like the symbol's namespace and/or function signature.

For example, given this C++ code:

namespace Foo {
namespace Bar {
    class A {
        public:
            A();
            ~A();
    };
}

int f(int i)
{
    Bar::A a;
}

int f(double d)
{
    Bar::A a;
}

}

the g++ compiler on a Linux box might generate the following "mangled" names for the 2 overloads of f() and A's constructor and destructor:

  • _ZN3Foo1fEd
  • _ZN3Foo1fEi
  • _ZN3Foo3Bar1AC1Ev
  • _ZN3Foo3Bar1AD1Ev

Using a tool like c++filt, we can decode these names:

$ echo _ZN3Foo1fEd | c++filt
Foo::f(double) 

$ echo _ZN3Foo3Bar1AC1Ev | c++filt
Foo::Bar::A::A() 

Of course, if you're not using g++ on Linux, the mangled form for your symbols may (likely WILL) be different. (The C++ FAQ lite subtly suggests that if two C++ compilers have different ABIs, even in small details, then they should intentionally have different name mangling schemes, so that things break in an obvious way and not a subtle way.)

316 questions
2101
votes
18 answers

What is the effect of extern "C" in C++?

What exactly does putting extern "C" into C++ code do? For example: extern "C" { void foo(); }
Litherum
  • 22,564
  • 3
  • 23
  • 27
608
votes
11 answers

How do I list the symbols in a .so file

How do I list the symbols being exported from a .so file? If possible, I'd also like to know their source (e.g. if they are pulled in from a static library). I'm using gcc 4.0.2, if that makes a difference.
Moe
  • 28,607
  • 10
  • 51
  • 67
150
votes
11 answers

Why do we need extern "C"{ #include } in C++?

Why do we need to use: extern "C" { #include } Specifically: When should we use it? What is happening at the compiler/linker level that requires us to use it? How in terms of compilation/linking does this solve the problems which…
Landon
  • 15,166
  • 12
  • 37
  • 30
140
votes
9 answers

Why can't C functions be name-mangled?

I had an interview recently and one question asked was what is the use of extern "C" in C++ code. I replied that it is to use C functions in C++ code as C doesn't use name-mangling. I was asked why C doesn't use name-mangling and to be honest I…
Engineer999
  • 3,683
  • 6
  • 33
  • 71
127
votes
15 answers

Unmangling the result of std::type_info::name

I'm currently working on some logging code that supposed to - among other things - print information about the calling function. This should be relatively easy, standard C++ has a type_info class. This contains the name of the typeid'd…
terminus
  • 13,745
  • 8
  • 34
  • 37
75
votes
10 answers

What is name mangling, and how does it work?

Please explain what is name mangling, how it works, what problem it solves, and in which contexts and languages is used. Name mangling strategies (e.g. what name is chosen by the compiler and why) a plus.
Stefano Borini
  • 138,652
  • 96
  • 297
  • 431
61
votes
2 answers

How can I see symbols of (C and C++) binary on linux?

Which tools do you guys use? How do demangle c++ symbols do be able to pass it to profiler tools, such as opannotate? Thanks
vehomzzz
  • 42,832
  • 72
  • 186
  • 216
55
votes
5 answers

Scala: How do I dynamically instantiate an object and invoke a method using reflection?

In Scala, what's the best way to dynamically instantiate an object and invoke a method using reflection? I would like to do Scala-equivalent of the following Java code: Class class = Class.forName("Foo"); Object foo = class.newInstance(); Method…
Eugene Yokota
  • 94,654
  • 45
  • 215
  • 319
52
votes
3 answers

Getting mangled name from demangled name

Is there any way to get back the mangled name from demangled name in g++. For example , I have the demangled name func(char*, int), what should I do to get the mangled name i.e _Z4funcPci back? My question is g++ specific.
Prasoon Saurav
  • 91,295
  • 49
  • 239
  • 345
49
votes
4 answers

C++ name mangling decoder for g++?

is there any C++ name-mangling decoder for g++?
SunnyShah
  • 28,934
  • 30
  • 90
  • 137
42
votes
2 answers

g++ undefined reference although symbol is present in *.so file

I found a number of similar questions (e.g. this, that or this), but none of them helped me solve my problem. I have a *.so file (from the core of gnss-sdr) that, as indicated by: $nm libgnss_system_parameters_dyn.so | c++filt |grep…
Ash
  • 4,611
  • 6
  • 27
  • 41
38
votes
1 answer

Is it possible to explicitly call a name mangled function?

Suppose I have something along the lines of struct Foo { void goo() {printf("Test");} } external void _ZN3Foo3gooEv(Foo *f); int main() { Foo f; _ZN3Foo3gooEv(&f); } Is it possible to call Foo::goo() through the name mangled…
Henry
  • 495
  • 3
  • 11
36
votes
2 answers

Why is name mangling not standardized

I'm just wondering why name mangling was never standardized by the C++ standard. Clearly having different name mangling algorithms hurts interoperability[1] and I don't see any advantage of having it implementation-defined. That is, contrary to say…
Voo
  • 29,040
  • 11
  • 82
  • 156
32
votes
2 answers

Can objdump un-mangle names of C++ template functions?

I have a C++ object file that contains instantiations of some C++ template functions. The object file in question instantiates the same function for a few different combinations of template parameters. I'm trying to debug a problem and would like to…
Jason R
  • 11,159
  • 6
  • 50
  • 81
32
votes
4 answers

How do I stop name-mangling of my DLL's exported function?

I'm trying to create a DLL that exports a function called "GetName". I'd like other code to be able to call this function without having to know the mangled function name. My header file looks like this: #ifdef __cplusplus #define EXPORT extern…
Slapout
  • 3,759
  • 5
  • 40
  • 61
1
2 3
21 22