Name Mangling and Type-safe Linkage in C/C++
Tags: cpp, overload, mangling, linkage
Published: 2021-01-19 17:51:43

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/obqt3yy6qt8yyx1ojivq.png)

## **What is Name Mangling?**
Notice that `C++` supports function overloading. Therefore, there can be more than one functions with same name and differences in parameters. The question to point out is how the `C++ compiler` distinguishes between functions when it generates object code. The solution to this problem is that `C++` changes name by adding information about arguments. Additional information added to functions is called `Name Mangling`. 

Let's have a look at declarations of function `f()`:
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/x5lyht5q4ff69zkqbzrz.PNG)
`C++ compiler` mangle above names to following
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/jopkxonkhu1rltmu8jpj.PNG)

## **Handle C symbols when linking from C++**
`C` does not support function overloading, which names may not be mangled. This is called linking problems. Let's have a look at a `C++` program that uses `printf()` function of `C`:
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/r7j1b00ma7e6fz53ydwu.PNG)
In later versions of `GCC` (GNU Compiler Collection) or the more common `MinGW` (Minimalist GNU for Windows), which supports both `C` and `C++`, no error may be given.

However, an expected error of `printf()` if compiled in earlier versions of compilers is `undefined reference to 'printf(char const*, ...)' ld returned 1 exit status`. 

The reason for this error is that `printf` is changed by `C++` compiler and it does not find definition of the function with new name.

Luckily, we still have a solution to this problem: extern "C" in C++. Change the above program to following, the program works fine and prints `Lam Nguyen` on console.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/661dxq1vsvxw3ygp202n.PNG)
When codes are put in `extern "C"` block, `C++` compiler ensures that the function names are unmangled - that the compiler emits a binary file with their names unchanged, as a `C` compiler would do.
Notice that because of the "unmangled" property, all `C` style header files (`stdio.h`, `string.h`, .. etc) have their declarations in `extern "C"` block;

**Note:** If you can compile the `C++` program without having to extern C in C++, let's give a round of applause to the developers of cross-language compiler!

## **What is Type-safe Linkage**
Type-safe linkage enforces the right number and type of parameters are passed at link time.

For example, in `C`, you can define a library function as taking an `int`, but through a series of mishaps/accidents, you can pass it as a string.

`C++` avoids this kind of error by **making function prototypes mandatory** and using name mangling to enforce type-safe linking.

Let's look at an example:
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/jjmmtxrik4u0yvlzit14.PNG)
Function here is actually `f(int)`, the compiler does not know this because it was told through an explicit declaration - that the function is `f(char)`. In `C`, the linker would also be successful, but not in `C++`.

I believe the above information about Type-safe is rather shallow, and require deeper research into the hidden mechanisms of it!

**Reference:** [CPP Forum](http://www.cplusplus.com/forum/general/12851/)