且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

范围和链接有什么区别?

更新时间:2023-11-09 11:33:52

"scope" 是编译器的命名空间;链接"是关于编译单元.

"scope" is a namespace of the compiler; "linkage" is about compiled units.

我再解释一下:在函数中声明的变量具有该函数的作用域,即它仅在该函数内可见.在源文件中声明为静态的变量只能由该源文件(以及所有包含的文件!)中的代码看到.变量也可以具有全局作用域:它们可以在源文件中引用,但不在该源文件中声明(分配),而是在另一个源文件中声明.

I explain a bit more: A variable declared in a function has the scope of that function, i.e. it is visible only within that function. A variable declared as static in a source file, can be seen only by the code in that source file (and all included files!). Variables can also have global scope: they can be referred to in a source file, but not declared (allocated) in that source file but declared in another source file.

我们应该说编译单元"而不是源文件",因为它是被编译的 C 源文件,加上所有包含的文件.范围是指编译器在编译单元中可以看到"的所有内容.这些是命名空间.

In stead of "source file" we should say "compilation unit" as it is the C source file being compiled, plus all included files. Scope refers to everything the compiler can "see" in a compilation unit. These are namespaces.

一个项目编译后有许多目标文件,每个编译单元一个.每个都可以引用未在编译单元中声明的变量.链接器现在必须解析目标文件之间的这些引用:链接.

After compilation of a project there are a number of object files, one for each compile unit. Each may refer to variables used that are not declared in the compile unit. The linker must now resolve these references between object files: linkage.

这也适用于函数.