c __cplusplus详解


Code:
#ifdef __cplusplus
extern "C" {
#endif
...
#ifdef __cplusplus
}
#endif
解释:1.c++中定义了__cplusplus,C语言中没有该定义。即:识别是c代码还是c++代码。

如下段代码:

#include <stdio.h>

int main(int argc,char *argv[])

{

#ifdef __cplusplus

printf("This is a c ++ program!\n");

#endif

#ifndef __cplusplus

printf("This is a c program!");

#endif

reutrn 0;

}

分别编译:gcc test.c

./a.out

g++ test.c

./a.out

看到程序输出内容你便知道了。

解释2.C语言和C++编译出来的函数不用,调用extern "C"会让c++编译器按照c的编译格式来编译。多用于c++库的头文件。


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM