• 被 extern 限定的函数或变量是 extern 类型的
  • 被 extern "C" 修饰的变量和函数是按照 C 语言方式编译和链接的
  • extern "C" 的作用是让 C++ 编译器将 extern "C" 声明的代码当作 C 语言代码处理,可以避免 C++ 因符号修饰导致代码不能和C语言库中的符号进行链接的问题。

在项目代码中看见这段代码:

#ifndef __XXX_H__
#define __XXX_H__
#ifdef __cplusplus extern "C" { #endif
/*...*/
#ifdef __cplusplus }; #endif

=================================================================

#ifdef __cplusplus //c++编译环境中才会定义__cplusplus (plus就是"+"的意思)
extern "C" {       //告诉编译器下面的函数是c语言函数(因为c++和c语言对函数的编译转换不一样,主要是c++中存在重载)
#endif

 

extern "C" 含义extern "C" 含义

 

相关文章:

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