经常在/usr/include目录下看到这种字句:

#ifdef __cplusplus
extern "C" {
#endif
...
#ifdef __cplusplus
}
#endif

不太明白是怎么用的。今天阅读autobook,在第53页看到了作者的解释:C/C++编译器对函数和变量名的命名方法不一样(例如C++中重载的函数有多个名字,而C的函数只有一个名字),这样当C编译器去引用C++编译器编译出来的符号时,会找不到链接。因此,当一个头文件可能既被C程序引用,又被C++程序引用时,需要使用如上代码进行区分。

上面的写法太复杂了,况且两个大括号{和}分离,会造成有些编辑器的缩进错误。更好的手法是将如下代码定义在一个公共头文件中,然后所有其它头文件去引用它:

#ifdef __cplusplus
#  define BEGIN_C_DECLS         extern "C" {
#  define END_C_DECLS           }
#else
#  define BEGIN_C_DECLS
#  define END_C_DECLS
#endif

有关__cplusplus和extern "C"的更多用法,可以参考下文:

《C++中extern “C”含义深层探索》
http://hi.baidu.com/17cpp/blog/item/a46bfd13a4816e025aaf53f6.html

__cplusplus的用法(转)的更多相关文章

  1. 【转】#ifdef __cplusplus+extern "C"的用法

    时常看到别人的头文件中,有这样的代码: #ifdef __cplusplus extern "C" { #endif //一段代码 #ifdef __cplusplus } #en ...

  2. extern "c"用法解析

    转自: extern "c"用法解析 - 简书 引言 C++保留了一部分过程式语言的特点,因而它可以定义不属于任何类的全局变量和函数.但是,C++毕竟是一种面向对象的程序设计语言, ...

  3. 解决全局变量共享---C语言的extern关键字用法

    在调试程序时,有一个参数需要在多个函数之间传递,因为是作为调试参数,不想将参数引入到函数中. 很自然的想到使用全局变量来表示这个公共参数,工程代码的结构如下: main.c test.c test.h ...

  4. extern "C" 用法解析

    extern "c"用法解析 作者 作者Jason Ding ,链接http://www.jianshu.com/p/5d2eeeb93590 引言 C++保留了一部分过程式语言的 ...

  5. #define用法集锦

    Definition: The #define Directive You can use the #define directive to give a meaningful name to a c ...

  6. extern “C”原理,用法以及使用场景-2016.01.05

    1 问题提出 在编程过程中,经常发现如下用法: #ifndef _FILE_NAME_H_ #define _FILE_NAME_H_ #ifdef __cplusplus extern " ...

  7. extern "C"的用法解析(转)

    原文链接:http://www.cnblogs.com/rollenholt/archive/2012/03/20/2409046.html   1.引言 C++语言的创建初衷是“a better C ...

  8. 【转载】extern "C"的用法解析(原博主就是抄百度百科的,不如另外一篇好)

    [说明]文章转载自Rollen Holt 的文章 http://www.cnblogs.com/rollenholt/archive/2012/03/20/2409046.html --------- ...

  9. #ifdef __cplusplus extern c #endif 的作用

    #ifdef __cplusplus // C++编译环境中才会定义__cplusplus (plus就是"+"的意思) extern "C" { // 告诉编 ...

随机推荐

  1. 深度学习(Deep Learning)算法简介

    http://www.cnblogs.com/ysjxw/archive/2011/10/08/2201782.html Comments from Xinwei: 最近的一个课题发展到与深度学习有联 ...

  2. 【R】函数-数学函数

  3. The future of scripting in Unity

    Recently we talked about Unity and WebGL . In that post we briefly spoke about how scripting works i ...

  4. gl.h included before glew.h

      So I'm trying to move my OpenGL code from Main() into a specific class that will handle the 3D gra ...

  5. curl_setopt — 设置 cURL 传输选项

    curl_setopt (PHP 4 >= 4.0.2, PHP 5, PHP 7) curl_setopt — 设置 cURL 传输选项 bool curl_setopt ( resource ...

  6. mysql下载安装使用教程

    今天花了点时间终于把mysql安装好了,哈哈!下面上干货! (1)从官网下载mysql.地址https://www.mysql.com/ 图:按照图上的步骤就能找到 a.首页点击    DOWNLOA ...

  7. 【转】一些linux基础命令

    学习Linux,其实很多基础命令很重要. 不论多么复杂的shell或者命令组合,都是一个一个的拼接组合命令拼接而成: 大号一个基本功,遇到需要的场景,信手拈来,随意组合拼接,是非常重要的. 恰好看到一 ...

  8. miniOrm

    PetaPoco.Core https://www.nuget.org/packages/PetaPoco.Core/5.1.228

  9. java.net.ConnectException: Connection refused: no further information

    NIO项目中出现了这个错误: java.net.ConnectException: Connection refused: no further information 一般是因为InetSocket ...

  10. MongoDB numa系列问题一:[initandlisten] connection refused because too many open connections:

    1:Mongod日志有很多这样的报错: [initandlisten] connection refused because too many open connections: 2:查看系统的限制 ...