为什么不外部链接到静态变量? [英] Why won't extern link to a static variable?

查看:30
本文介绍了为什么不外部链接到静态变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么在声明 n 时 extern int n 不编译(在不同的文件中)static int n,但在声明 int n 时有效>?(这两个声明都在文件范围内.)

Why does extern int n not compile when n is declared (in a different file) static int n, but works when declared int n? (Both of these declarations were at file scope.)

基本上,为什么文件范围内的 int n 与同一范围内的 static int n 不一样?它仅与外部有关吗?如果是这样,我缺少 extern 怎么办?

Basically, why is int n in file scope not the same as static int n in the same scope? Is it only in relation to extern? If so, what about extern am I missing?

推荐答案

static 的全部和全部目的是声明一个变量对于声明它的源文件是 私有的在.因此,它在阻止来自外部的连接方面做得恰到好处.

The whole and entire purpose of static is to declare that a variable is private to the source file that it is declared in. Thus, it is doing precisely its job in preventing a connection from an extern.

请记住,文件范围变量定义有四种风格:

Keep in mind that there are four flavors of file-scope variable definition:

  1. int blah = 0; — blah 在此文件中定义并可从其他文件访问.其他文件中的定义是重复的,会导致错误.
  2. extern int blah; — blah 必须在别处定义并从此文件引用.
  3. int blah; — 这是 FORTRAN COMMON 的道德等价物.您可以在文件中包含任意数量的这些,并且它们都由链接器解析为一个共享的 int.(*)
  4. static int blah;(可选带有初始化程序)——这是静态的.这个文件是完全私有的.它对其他文件中的 extern 不可见,您可以有许多不同的文件,它们都声明 static TYPE blah;,并且它们都不同.
  1. int blah = 0; — blah is defined in this file and accessible from other files. Definitions in other files are duplicates and will lead to errors.
  2. extern int blah; — blah must be defined elsewhere and is referenced from this file.
  3. int blah; — This is the moral equivalent of FORTRAN COMMON. You can have any number of these in files, and they are all resolved by the linker to one shared int. (*)
  4. static int blah; (optionally with an initializer) — This is static. It is completely private to this file. It is not visible to externs in other files, and you can have many different files that all declare static TYPE blah;, and they are all different.

对于观众中的纯粹主义者:'file' = 编译单元.

For the purists in the audience: 'file' = compilation unit.

请注意,静态内部函数(不在文件范围内)的范围更加严格:如果两个函数声明 static int bleh = 0; 即使在同一个文件中,它们也是不相关的.

Note that static inside functions (not at file scope) are even more tightly scoped: if two functions declare static int bleh = 0; even in the same file, they are unrelated.

(*):对于那些不熟悉的人:在通常的模式中,一个编译单元必须定义一个全局变量,其他人可以引用它.它存在"在那个编译单元中.在上面的情况 (3) 中,没有文件(或所有文件)定义它.如果两个文件说 int blah = 0;,链接器将抱怨多个定义.如果两个文件说 int blah; 链接器会愉快地创建一个全局 int 并导致所有代码引用它.

(*): for those of you not familiar: in the usual pattern, one compilation unit has to define a global variable, and others can reference it. It 'lives' in that compilation unit. In case (3), above, no file (or all the files) defines it. If two files say int blah = 0;, the linker will complain of multiple definitions. If two files say int blah; the linker cheerfully creates a single global int and causes all the code to refer to it.

这篇关于为什么不外部链接到静态变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆