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++库的头文件。

相关文章:

  • 2021-11-06
  • 2021-07-07
  • 2022-12-23
猜你喜欢
  • 2021-12-28
  • 2022-12-23
  • 2022-01-28
  • 2021-08-18
  • 2022-03-03
  • 2021-08-02
相关资源
相似解决方案