原文链接:http://www.geeksforgeeks.org/extern-c-in-c/

C++ supports function overloading, i.e., there can be more than one functions with same name and differences in parameters. How does C++ compiler distinguishes between different functions when it generates object code – it changes names by adding information about arguments. This technique of adding additional information to function names is called Name Mangling. C++ standard doesn’t specify any particular technique for name mangling, so different compilers may append different information to function names.

Consider following declarations of function f()

1 int  f (void) { return 1; }
2 int  f (int)  { return 0; }
3 void g (void) { int i = f(), j = f(0); }

A C++ compiler may mangle above names to following (Source: Wiki)

1 int  __f_v (void) { return 1; }
2 int  __f_i (int)  { return 0; }
3 void __g_v (void) { int i = __f_v(), j = __f_i(0); }

How to handle C symbols when linking from C++?
In C, names may not be mangled as C doesn’t support function overloading. So how to make sure that name of a symbol is not changed when we link a C code in C++. For example, see the following C++ program that uses printf() function of C.

1 // Save file as .cpp and use C++ compiler to compile it
2 int printf(const char *format,...);
3
4 int main()
5 {
6     printf("GeeksforGeeks");
7     return 0;
8 }

Output:

 1 undefined reference to `printf(char const*, ...)'  ld returned 1 exit status 

The reason for compiler error is simple, name of printf is changed by C++ compiler and it doesn’t find definition of the function with new name.

The solution of problem is extern “C” in C++. When some code is put in extern “C” block, the 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.

If we change the above program to following, the program works fine and prints “GeeksforGeeks” on console.

 1 // Save file as .cpp and use C++ compiler to compile it
 2 extern "C"
 3 {
 4     int printf(const char *format,...);
 5 }
 6
 7 int main()
 8 {
 9     printf("GeeksforGeeks");
10     return 0;
11 }

Output:

 1 GeeksforGeeks 

Therefore, all C style header files (stdio.h, string.h, .. etc) have their declarations in extern “C” block.

1 #ifdef __cplusplus
2 extern "C" {
3 #endif
4     /* Declarations of this file */
5 #ifdef __cplusplus
6 }
7 #endif

Following are main points discussed above
1. Since C++ supports function overloading, additional information has to be added to function names (called name mangling) to avoid conflicts in binary code.
2. Function names may not be changed in C as C doesn’t support function overloading. To avoid linking problems, C++ supports extern “C” block. C++ compiler makes sure that names inside extern “C” block are not changed.

转载于:https://www.cnblogs.com/lyleslie/p/4829290.html

Name Mangling and extern “C” in C++相关推荐

  1. [C/CPP系列知识] C++中extern “C” name mangling -- Name Mangling and extern “C” in C++

    http://www.geeksforgeeks.org/extern-c-in-c/ C++函数重载(function overloading),但是C++编译器是如何区分不同的函数的呢?----是 ...

  2. C++ name mangling

    name mangling在wiki上的定义:它提供了在函数.结构体.类或其它的数据类型的名字中编码附加信息一种方法,用于从编译器中向链接器传递更多语义信息. 简单来说就是在编译阶段将函数名进行转换, ...

  3. 名称的特殊处理(Name Mangling)

    先说一个事情, mangle 的意思是 vt.乱砍, 损坏; n. 碾压机. 这意味着 name mangling 就是要先把你精心想出的名字们碾碎, 再拼成独一无二的样子, 当然这么残忍的事情都是编 ...

  4. extern C 的作用

    有些朦朦胧胧 特上网搜了一下 extern   "C"是告诉C++编译器以C   Linkage方式编译,也就是抑制C++的name   mangling机制.

  5. extern “C”总结

    学了就忘,忘了再学.有时感觉自己几乎就是一个跳梁小丑.没有办法,只好多记些笔记. 本文版权归作者和博客园共有 FROM: http://www.cnblogs.com/graphics/archive ...

  6. c++起始(名词修饰,extern “C” ,引用)

    名字修饰(name Mangling) 在C/C++中,一个程序要运行起来,需要经历以下几个阶段:预处理.编译.汇编.链接. Name Mangling是一种在编译过程中,将函数.变量的名称重新改编的 ...

  7. C语言易混淆关键词详解-const, static, extern, typedef, 声明

    Const Const关键词并不能把一个变量变成一个常量, 在符号前加上const表示这个符号不能被赋值, 即他的值对这个符号来说是只读的, 但并不代表这个值不能用其他方法去改变. 通过下面的例子就能 ...

  8. 电脑粉碎文件 c语言,C/C++ 编译器的命名粉碎规则(name mangling)和C/C++混合(ZZ)

    http://hi.baidu.com 在面向对象编程语言出现之前,如果你想要打印不同类型的数据,需要写多个方法 ,象是PrintInteger(int i),PrintString(string s ...

  9. 关于extern “C“(详细剖析)

    目录 引言 extern "C"的前世今生 小心门后的未知世界 c++调用c的方法 c调用c++的方法 引言 在你工作过的系统里,不知能否看到类似下面的代码. 这好像没有什么问题, ...

  10. extern “C“详解

    转载于 https://blog.csdn.net/u010639500/article/details/87885421 仅用于学习记录 ,若侵权告知立删 [目录] 引言 extern " ...

最新文章

  1. 微服务架构10条最佳实践
  2. haproxy mysql配置文件详解_HAProxy基础配置-haproxy的配置文件说明
  3. hsv 明度的范围_通过HSV转换的方式实现图片数据增强
  4. 父、子页面之间页面元素的获取,方法的调用
  5. oracle for net,使用Oracle Developer Tools For Visual Studio .NET-.NET教程,数据库应用
  6. java非负整数怎么设_使用JAVA将非负十进制整数n转换成b进制,递归方法与非递归方法...
  7. c#关于委托和事件(二)(介绍的很详细)
  8. 拓端tecdat|R语言中自编基尼系数的CART回归决策树的实现
  9. Python 寻找完美数
  10. android+世界地图高清版大图片,世界地图全图高清版
  11. 第五人格显示服务器维护中请稍后登录怎么办,《第五人格》未知异常 请稍后重试登不上去如何解决...
  12. img 格式相互转换 url, base64,blob, file
  13. Oracle EBS财务模块(三)总账功能
  14. PCB及电路抗干扰措施
  15. java数组初始化的方式_java数组初始化方式
  16. 软件开发人员转型指南
  17. ubuntu mysql密码忘记了怎么办,ubuntu怎么查看mysql密码
  18. k8s NoSchedule k8s 设置污点 NoExecute有点复杂 移除节点 drain和不可调度cordon
  19. 下载MySQL历史版本
  20. dom4j将word转pdf详细介绍(亲测可用)

热门文章

  1. [Android]Webview中JS接口调用Java-版本问题
  2. 【转】移动前端工作的那些事---UE/UI架构原型搭建和前端设计之UE架构原型搭建...
  3. 线性规划 整数规划 01规划
  4. 如何移除照片中不需要的东西?这三款Mac App软件不会让你失望!
  5. iOS WKWebview JS与OC互相调用,传递数据
  6. 微任务,宏任务和Event-Loop
  7. cost of path 的计算
  8. Python- 索引 B+数 比如书的目录
  9. MongoDB简单使用 —— 安装
  10. Elasticsearch模块功能之-索引分片分配(Index shard allocation)