C++ 中的名称修饰和 Extern "C"

2024年8月28日 | 阅读 4 分钟

Java 和 C++ 编程语言都分别支持方法重载和函数重载。函数重载简单来说就是拥有多个同名函数,这些函数通过参数数量或参数数据类型的不同来区分。真正的问题是,Java 和 C++ 编程语言的编译器如何区分这些不同的函数。

对于这个复杂问题,一个简单的答案是,编译器会自行添加更多信息。在执行期间,它会将方法或参数保存到其栈内存中。C++ 编译器并没有什么与其他编译器不同的特定技术,它使用相同的名称修饰(name-mangling)概念来解决函数重载的问题。

C++ 中的函数重载 -1

输出

The sum of two integer variables is= 222
The sum of the two double variables is= 121.5

C++ 中的函数重载 -2

输出

The sum of two integer variables is= 430
The sum of three integer variables is= 165

C++ 中的函数重载 -3

输出

 Here we have the integer  100
 Here we have the float 100.1
 Here we have the character hundred

C++ 中的名称修饰和 Extern 'C'

演示代码

输出

/usr/bin/ld: cannot open output file a.out: Permission denied
collect2: error: ld returned 1 exit status

C++ 代码

输出

/usr/bin/ld: cannot open output file a.out: Permission denied
collect2: error: ld returned 1 exit status

Extern C

输出

javaTpoint