[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++编译器是如何区分不同的函数的呢?----是通过在函数名是加些信息来区不同的函数,即所谓的Name Mangling。C++标准并没有对name mangling技术,各个编译器可以添加不同的信息。
考虑下面的函数
int f (void) { return ; }
int f (int) { return ; }
void g (void) { int i = f(), j = f(); }
C++编译器也许会重命名为 (Source: Wiki)
int __f_v (void) { return ; }
int __f_i (int) { return ; }
void __g_v (void) { int i = __f_v(), j = __f_i(); }
C++链接器如何处理C语言中的符号呢?
C语言不支持函数重载,所以没有name mangling技术,那么在C++中使用了C函数后如何保证C++链接器能够正取的链接呢?
// Save file as .cpp and use C++ compiler to compile it
int printf(const char *format,...); int main()
{
printf("GeeksforGeeks");
return ;
}
编译结果:
diego@ubuntu:~/myProg/geeks4geeks/cpp$ g++ test11.cpp
test11.cpp::: error: invalid preprocessing directive #int
#int printf(const char *format,...);
^
test11.cpp: In function 'int main()':
test11.cpp::: error: 'printf' was not declared in this scope
printf("GeeksforGeeks\n");
^
diego@ubuntu:~/myProg/geeks4geeks/cpp$
编译错误的原因是C++编译器对printf函数进行了name mangling,然后找不到重命名后的函数的符号。解决办法就是使用extern "C" 关键字。
// Save file as .cpp and use C++ compiler to compile it
extern "C"
{
int printf(const char *format,...);
} int main()
{
printf("GeeksforGeeks");
return ;
}
输出结果:
diego@ubuntu:~/myProg/geeks4geeks/cpp$ g++ test11.cpp
diego@ubuntu:~/myProg/geeks4geeks/cpp$ ./a.out
GeeksforGeeks
所以,所有的C语言的库函数都要包含在extern “C” block中。
#ifdef __cplusplus
extern "C" {
#endif
/* Declarations of this file */
#ifdef __cplusplus
}
#endif
[C/CPP系列知识] C++中extern “C” name mangling -- Name Mangling and extern “C” in C++的更多相关文章
- [C/CPP系列知识] 在C中使用没有声明的函数时将发生什么 What happens when a function is called before its declaration in C
http://www.geeksforgeeks.org/g-fact-95/ 1 在C语言中,如果函数在声明之前被调用,那么编译器假设函数的返回值的类型为INT型, 所以下面的code将无法通过编译 ...
- [C/CPP系列知识] 那些程序C语言可以编译通过但C++无法编译成功 Write a C program that won’t compile in C++
http://www.geeksforgeeks.org/write-c-program-wont-compiler-c/ 1) C++中在函数声明之前调用一个函数会引发错误,但是在C中有可能可以. ...
- [C/CPP系列知识] Type difference of character literals 和 bool in C and C++
C/C+中的每一个常亮(every literal)都是有类型的,例如10 就是int型的,因此siziof(10)和sizeof(int)是相同的,但是字符型常亮(‘a’)在C和C++中有不同的变量 ...
- 浏览器扩展系列————在WPF中定制WebBrowser快捷菜单
原文:浏览器扩展系列----在WPF中定制WebBrowser快捷菜单 关于如何定制菜单可以参考codeproject上的这篇文章:http://www.codeproject.com/KB/book ...
- WebApi系列知识总结
WebApi系列知识 一.webApi项目搭建 1.新建WebApi项目 (1) (2) (3) (4) Areas – HelpPage – App_Start – HelpPageConfig.c ...
- css3系列-2.css中常见的样式属性和值
css3系列-2.css中常见的样式属性和值 继续上一篇文章的继续了解css的基础知识,关注我微信公众号:全栈学习笔记 css中常见的样式属性和值 字体与颜色 背景属性 文本属性 边框属性 内外边距 ...
- css3笔记系列-3.css中的各种选择器详解,不看后悔系列
点击上方蓝色字体,关注我 最详细的css3选择器解析 选择器是什么? 比较官方的解释:在 CSS 中,选择器是一种模式,用于选择需要添加样式的元素. 最常见的 CSS 选择器是元素选择器.换句话说 ...
- 20.翻译系列:Code-First中的数据库迁移技术【EF 6 Code-First系列】
原文链接:https://www.entityframeworktutorial.net/code-first/migration-in-code-first.aspx EF 6 Code-First ...
- 8.翻译系列: EF 6中配置领域类(EF 6 Code-First 系列)
原文地址:http://www.entityframeworktutorial.net/code-first/configure-classes-in-code-first.aspx EF 6 Cod ...
随机推荐
- Professional iOS Network Programming Connecting the Enterprise to the iPhone and iPad
Book Description Learn to develop iPhone and iPad applications for networked enterprise environments ...
- c# list排序
List<int> tmp = new List<int>(){5,1,22,11,4}; 升序:tmp.Sort((x, y) => x.CompareTo(y)); ...
- Application 统计在线人数
MVC 统计在线人数: protected void Application_Start() { Application[; AreaRegistration.RegisterAllAreas(); ...
- 20141103--SQL主键,外键
设置主键: 右键表→设计,在需要的列上右键→设置主键 或者在创建表的时候,在想要作为索引的列后面加上 primary key create table biao3 ( [No.] int primar ...
- 九款酷炫基于jquery实现的应用及源码
1.HTML5 Loading动画加载 五彩的圆环Loading 今天我们要分享一款基于HTML5的Loading加载动画特效,这款HTML5加载动画是一个五彩的圆环,圆环不停地转动从而体现加载正在进 ...
- php判断是否为手机客户端
function isWap() { $http_agent = isset ( $_SERVER ["HTTP_USER_AGENT"] ) ? $_SERVER ...
- Linux 如何设置只允许域名访问站点而禁止IP访问站点
最近在论坛里看到有人问到 Linux 如何设置只允许域名访问站点而禁止IP访问站点的问题,之前自己也用过这个功能,可以防止别人用 IP 地址来访问到自己的网站,下面我就我自己的环境给出解决方法,我用的 ...
- js根据IP取得天气
<span id="weather"></span> <script> function weather(cityName) { var cha ...
- php实现input输入框失去焦点自动保存输入框的数据
最近做一个输入框失去焦点时自动保存数据的功能,当然就是jQuery选择器选择input,blur时,ajax提交数据给php文件,php文件保存一下数据咯.主要是要注意一下中文的问题,所以中间需要转一 ...
- centos安装mysql(rpm)
今天安装mysql时候出现错误MySQL conflicts with mysql-4.1.20-2 查看是否安装过mysql rpm -qa mysql 发现安装了老版本的mysql 使用rpm - ...