__cplusplus 含义
来源:互联网 发布:王国纪元mac电脑版 编辑:程序博客网 时间:2024/05/02 04:33
经常在/usr/include目录下看到这种字句:
Code:
#ifdef __cplusplus
extern "C" {
#endif
...
#ifdef __cplusplus
}
#endif
不太明白是怎么用的。今天阅读autobook,在第53页看到了作者的解释:C/C++编译器对函数和变量名的命名方法不一样(例如C++中重载的函数有多个名字,而C的函数只有一个名字),这样当C编译器去引用C++编译器编译出来的符号时,会找不到链接。因此,当一个头文件可能既被C程序引用,又被C++程序引用时,需要使用如上代码进行区分。
上面的写法太复杂了,况且两个大括号{和}分离,会造成有些编辑器的缩进错误。更好的手法是将如下代码定义在一个公共头文件中,然后所有其它头文件去引用它:
Code:
#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
Code:
#ifdef __cplusplus
extern "C" {
#endif
...
#ifdef __cplusplus
}
#endif
不太明白是怎么用的。今天阅读autobook,在第53页看到了作者的解释:C/C++编译器对函数和变量名的命名方法不一样(例如C++中重载的函数有多个名字,而C的函数只有一个名字),这样当C编译器去引用C++编译器编译出来的符号时,会找不到链接。因此,当一个头文件可能既被C程序引用,又被C++程序引用时,需要使用如上代码进行区分。
上面的写法太复杂了,况且两个大括号{和}分离,会造成有些编辑器的缩进错误。更好的手法是将如下代码定义在一个公共头文件中,然后所有其它头文件去引用它:
Code:
#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
0 0
- __cplusplus 含义
- 解译“_ifdef+__cplusplus+”的含义
- //#ifdef __cplusplus extern "C" 含义
- #ifdef __cplusplus extern "C" { #endif 含义
- __cplusplus
- __cplusplus
- __cplusplus
- __cplusplus
- __cplusplus
- __cplusplus
- __cplusplus
- #ifdef __cplusplus extern "C" { #endif”的定义的含义
- #ifdef __cplusplus extern "C" { #endif”的定义的含义
- #ifdef __cplusplus extern "C" { #endif”的定义的含义 .
- #ifdef __cplusplus extern "C" { #endif”的定义的含义
- C++中extern"C"深层含义&&#ifdef __cplusplus 倒底是什么意思?
- #ifdef __cplusplus
- __cplusplus 用法
- 心跳检测
- gsoap使用实例
- 用gsoap实现WebService
- PHP curl模块
- 为VS2005模板添加自定义注释
- __cplusplus 含义
- 声明函数指针并实现回调
- 函数的默认参数重复定义问题
- csting and sting 互转
- 如何用google analytics跟踪和统计CSDN上blog的访问数据
- CString对象的连接和格式化字符串
- 30岁之后的程序员
- C/C++常用宏定义,注意事项,宏中#和##的用法 .
- c/c++不定参数函数详解