[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 ...
随机推荐
- OC5_@class关键字
// // ClassB.h // OC5_@class关键字 // // Created by zhangxueming on 15/6/24. // Copyright (c) 2015年 zha ...
- python基础:搜索路径
如何将写好的脚本或者是模块加入python的搜索路径? >>>import sys >>> sys.path ['', '/Library/Frameworks/P ...
- [GeekBand]C++高级编程技术(2)
本篇笔记主要分为两个主要部分,第一部分关于对象模型,第二部分是关于new和delete的更加深入的学习. 一.对象模型 关于vptr(虚指针)和vtbl(虚函数表) 只要用到了虚函数,对象中就会多一个 ...
- shiro错误No SecurityManager accessible to the calling code
Shire在Web.xml中shiroFilter的Mapping配置错误 org.apache.shiro.UnavailableSecurityManagerException: No Secur ...
- 《linux 网卡别名的添加和绑定》RHEL6
网卡别名的配置: 这个和ifconfig临时修改网卡ip 差不多,但是不一样.都是临时的,只要重启电脑就没了. 配永久的ip别名: cp ifcfg-eth0 ifcfg-eth0:0 vim if ...
- Ubuntu14.04 安装 PHP cURL
今天遇到 Fatal error: Call to undefined function curl_init() in /xxx/xxxx/www/application/library/Ku/Htt ...
- JavaScript 同源策略
在JavaScript安全性限制Same-Origin Policy(同源策略)=>JavaScript只能访问与包含它的文档在同一域下的内容. 同源=>域名+协议+端口相同.
- ORACLE 语句关联统计
很久不用SQL语句了,貌似入职新公司后,又回归到了三年前的SQL时代,一写一坨的SQL好吧,也当回归一下过去的知识. 下面是统计2月份某数据的计费统计 select t.telno as 主号,VID ...
- js正则学习及一些正则集合
正则中文版详细说明请看中文版w3cschool-----http://www.w3school.com.cn/jsref/jsref_obj_regexp.asp微软正则表达式语言-快速参考:http ...
- smarty安装及例子
环境: smarty3.1.16 1.在http://www.smarty.net/download下载最新smarty包,window选择zips,linux下选择tar.gz.以windows为例 ...