msdn随笔笔记(四)-extern;Linkage Specifications;Linkage;CCriticalSection

本文详细介绍了C++中的extern关键字如何指定外部链接,以及Linkage规范的作用,包括C++中如何使用extern "C"进行跨语言链接。同时,文章提及了CCriticalSection在多线程中的应用,用于保护资源的独占访问。
摘要由CSDN通过智能技术生成

1.Using extern to Specify Linkage

Syntax
linkage-specification :
extern declarator
extern string-literal { declaration-listopt }
extern string-literal declaration
declaration-list :
declaration
declaration-list declaration
The extern keyword declares a variable or function and specifies that it has external linkage (its name is visible from files other than the one in which it's defined). When modifying a variable, extern specifies that the variable has static duration (it is allocated when the program begins and deallocated when the program ends). The variable or function may be defined in another source file, or later in the same file. Declarations of variables and functions at file scope are external by default.
For example:
int i = 1;
void other( void );
void main(){
  /* Reference to i, defined above: */
  extern int i;
  return;
}
void other( void ){
/* Address of global i assigned to pointer variable: */
  static int *external_i = &i;
/* i is redefined; global i no longer visible: */
  int i = 16;
In C++, when used with a string, extern specifies that the linkage conventions of another language are being used for the declarator(s). C functions and data can be accessed only if they are previously declared as having C linkage. However, they must be defined in a separately compiled translation unit.
Microsoft C++ supports the strings "C" and "C++" in the string-literal field. All of the standard include files use the extern "C" syntax to allow the run-time library functions to be used in C++ programs.
The following example shows alternative ways to declare names that have C linkage:
// Declare printf with C linkage.
extern "C" int printf( const char *fmt, ..

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值