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++的更多相关文章

  1. [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将无法通过编译 ...

  2. [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中有可能可以. ...

  3. [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++中有不同的变量 ...

  4. 浏览器扩展系列————在WPF中定制WebBrowser快捷菜单

    原文:浏览器扩展系列----在WPF中定制WebBrowser快捷菜单 关于如何定制菜单可以参考codeproject上的这篇文章:http://www.codeproject.com/KB/book ...

  5. WebApi系列知识总结

    WebApi系列知识 一.webApi项目搭建 1.新建WebApi项目 (1) (2) (3) (4) Areas – HelpPage – App_Start – HelpPageConfig.c ...

  6. css3系列-2.css中常见的样式属性和值

    css3系列-2.css中常见的样式属性和值 继续上一篇文章的继续了解css的基础知识,关注我微信公众号:全栈学习笔记 css中常见的样式属性和值 字体与颜色 背景属性 文本属性 边框属性 内外边距 ...

  7. css3笔记系列-3.css中的各种选择器详解,不看后悔系列

    点击上方蓝色字体,关注我 最详细的css3选择器解析 ​ 选择器是什么? 比较官方的解释:在 CSS 中,选择器是一种模式,用于选择需要添加样式的元素. 最常见的 CSS 选择器是元素选择器.换句话说 ...

  8. 20.翻译系列:Code-First中的数据库迁移技术【EF 6 Code-First系列】

    原文链接:https://www.entityframeworktutorial.net/code-first/migration-in-code-first.aspx EF 6 Code-First ...

  9. 8.翻译系列: EF 6中配置领域类(EF 6 Code-First 系列)

    原文地址:http://www.entityframeworktutorial.net/code-first/configure-classes-in-code-first.aspx EF 6 Cod ...

随机推荐

  1. 20171107--SQL变量,运算符,存储过程

    create database shujuku03 go use shujuku03 go create table jiaoshi--创建jiaoshi表-- ( code int primary ...

  2. 解决Toad for Oracle显示乱码问题

    1.查看一下数据库字符集: 使用下面语句:Select userenv('language') from dual;或者:Select name, value$ from props$;查看. 查看完 ...

  3. QThread多线程编程经典案例分析

    传统的图形界面应用程序都只有一个线程执行,并且一次执行一个操作.如果用户调用一个比较耗时的操作,就会冻结界面响应. 一个解决方法是按照事件处理的思路: 调用 Void QApplication::pr ...

  4. 反射-Reflect

    1.得到一份Class(同一个类在JVM中只有一份字节码) 三种方式:类名.class, Class.forName("全限定名");, 对象.getClass(); 基本类型 i ...

  5. C++ 嵌入汇编 获取CPU信息

    #include "windows.h" #include "iostream" #include "string" using names ...

  6. NSS_05 数据访问选型

    在数据访问层上很想用orm框架, 选用Nhibernate或ef, 可以直接操作类对象, 避免转换, 更加的面向对象,更重要的是开发起来就方便多了. 但是从网上了解到这些框架太高级了, 用得不好到时会 ...

  7. this.IsMounted() is not a function

    I'm trying to build a simple React App. It retrieves data from an ajax call and renders it to the pa ...

  8. linux下文件的复制、移动与删除

    linux下文件的复制.移动与删除命令为:cp,mv,rm 一.文件复制命令cp     命令格式:cp [-adfilprsu] 源文件(source) 目标文件(destination)      ...

  9. ipc telnet 攻击

    ping %1 -n 2net use \\%1sc \\%1 config tlntsvr start= autosc \\%1 start tlntsvrtelnet %1

  10. mikrotik/IPSec Dynamic End points Updater.rsc

    # IPSec Peer/Policy Updater for Dynamic WAN addresses # ============================================ ...