Linkers, Loaders & Executables

What is the main purpose of name mangling, in which a C++ compiler encodes argument types and other information into symbol names?

  1. ATo make function names unreadable so the program cannot be stolen
  2. BTo shorten function names and make the executable smaller
  3. CTo let the linker distinguish overloaded functions with the same name but different argument types, and namespaces
Show answer and explanation

Answer: C. To let the linker distinguish overloaded functions with the same name but different argument types, and namespaces

The linker matches symbols purely by their name strings. C++ allows functions with the same name, such as print(int) and print(double), which would otherwise be indistinguishable. So the compiler embeds the argument types and more into the name, as in "_Z5printi". C functions are not mangled, so when C++ calls a C function, it is declared extern "C" to turn mangling off.

  • A. It is not a protection measure.
  • B. Names actually get longer.
  • C. Correct. The linker matches by name alone.
Practice this topic Back to topic

More Linkers, Loaders & Executables questions

  1. What is the most suitable use of Linux's dlopen function (the counterpart of Windows LoadLibrary)?
  2. When 100 processes use the same shared library, how is physical memory used?
  3. Disassembling an object file shows that an instruction calling the external function printf has a placeholder target such as 0. When is the correct value filled in?
  4. What is the role of a linker script used in embedded development?
  5. On a microcontroller without an OS, what does the start-up code do right after power-on before calling main?